简体   繁体   中英

Retrieving Namespaces from Element in Java (using DOM)

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.

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());
            }
        }
    }
}

I don't know if it's the appropriate way but what you could possibly do is to just take the attributes on OpenSearchDescription , find out if they are namespace declarations with the help of the constants located in javax.xml.XMLConstants eg XMLNS_ATTRIBUTE constant should be "xmlns". You could traverse all the attributes and save all attributes starting with that value as your NameSpaceUri's.

I think you should also have a look at the NameSpaceContext . I don't know how you load your document, but if you're using a XMLStreamReader then you can retrieve a NameSpaceContext from it that holds exactly the information you need. Here you can see where you can retrieve the NameSpaceContext in other ways.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM