Retrieving Namespaces from Element in Java (using DOM)

14,734

This might get you started, factory.setNamespaceAware(true) is the key for getting the namespace data after all.

public static void main(String[] args) throws ParserConfigurationException, SAXException,
        IOException
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File(args[0]));
    Element root = doc.getDocumentElement();
    //prints root name space
    printAttributesInfo((Node) root);

    NodeList childNodes = root.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++)
    {
        printAttributesInfo(childNodes.item(i));
    }
}

public static void printAttributesInfo(Node root)
{
    NamedNodeMap attributes = root.getAttributes();
    if (attributes != null)
    {
        for (int i = 0; i < attributes.getLength(); i++)
        {
            Node node = attributes.item(i);
            if (node.getNodeType() == Node.ATTRIBUTE_NODE)
            {
                String name = node.getNodeName();
                System.out.println(name + " " + node.getNamespaceURI());
            }
        }
    }
}
Share:
14,734
Buhake Sindi
Author by

Buhake Sindi

First person in South Africa to receive a Gold Java Badge (Received on 5th November 2011) and 71st overall on StackOverflow. Whoohoo! :-) I also founded Devoxx4Kids South Africa to promote technology education among children. Where can you find me? Sindi Technologies (Founder) My Blog (A Developer's Enterprise Log...) GitHub Twitter LinkedIn Google+ SoundCloud #SOreadytohelp The following shows the experiences I have with computer programming languages &amp; various frameworks. It is not a reflection of who I am. Professional experience: Enterprise Java (Jakarta EE/ Java EE, Spring Framework) JavaScript Frameworks: (Angular 1.x), ReactJS (JavaScript/TypeScript), jQuery VB .NET SQL (RDBMS independent) Web Technologies: HTML/XHTML/HTML 5, JavaScript, JavaScript/jQuery/Angular 1.x and higher --&gt; CSS (from version 1 to 3), including Bootstrap Framework External Experience: Pascal / Delphi VB C/C++ Assembly language Adobe Flex PHP Scala Go (Briefly played with)

Updated on July 19, 2022

Comments

  • Buhake Sindi
    Buhake Sindi almost 2 years

    I have the following XML example:

    <?xml version="1.0" encoding="UTF-8"?>
    <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
        xmlns:custom="http://example.com/opensearchextensions/1.0/">
        <ShortName>Test</ShortName>
        <Description>Testing....</Description>
        <Url template="whatever" type="what" />
        <Query custom:color="blue" role="example" />
    </OpenSearchDescription>
    

    My concern is with Query element. It has a Namespace attribute and, in java, you will need a namespaceURI to retrieve the value.

    My question is: How would I retrieve the list of namespaces from the root element (in this case, OpenSearchDescription element)? I want the attribute, prefix and namespace URI that I can use to request on Query.

    Thanks.

    PS: I'm using DOM in java, standard in Java SE. I'm willing to move to XPath, if it's possible. The requirement is that only the Java Standard API needs to be used.

  • Buhake Sindi
    Buhake Sindi over 12 years
    Thanks, I have the similar code. The thing is to always check if Node.getNamespaceURI() returns is not null. That's how I check. It works.