Search code examples
javaandroidxmltagsgetelementsbytagname

Android Java XML : how to find tag like "abc:tag" with unknown "abc" part with getElementsByTagName()


My Android app reads a XML file and searches for a tag that can be found in different forms, like "tag" or "abc:tag" for example. My code is able to find just the "tag" form. How to find other forms? (wildcards?)

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
...
...
NodeList nodeList = root.getElementsByTagName("tag");

Solution

  • The solution is to use a similar method: getElementsByTagNameNS(String namespaceURI,String localName) with special "*" namespace

    ...
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    ...
    NodeList nodelist=root.getElementsByTagNameNS("*","tag");