简体   繁体   中英

How to get specific node from XML with several namespaces using Java?

I want to validate XML that it has expected node value. But I can't get node I need. Please help me)

Here is my XML. I want to get the value of this node ns3:site

<soap-env:envelope xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap-env:body>
      <ns4:findsiteconfigurationbysmth xmlns:ns3="http://www.testsite.com/common" xmlns:ns2="http://www.testsite.com/plant" xmlns:ns4="someapi:com:plant" xmlns:ns5="someapi:com:reasoncode">
         <ns4:response>
            <ns2:ref>SiteWD:QWERTY</ns2:ref>
            <ns3:site>QWERTY</ns3:site>
            <ns3:description>test description</ns3:description>
            <ns3:timezone>Africa/Abidjan</ns3:timezone>
         </ns4:response>
      </ns4:findsiteconfigurationbysmth>
   </soap-env:body>
</soap-env:envelope>

I know somehow I have to handle with namespaces. I marketed them at my code below. It didn't help me.

I've tried this approach

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();

Document myXml = builder.parse(new File(PATH_TO_XML));

NodeList node = myXml.getDocumentElement().getElementsByTagNameNS("http://www.testsite.com/common", "ns3");

node.item(0);

At this case my result is null .

Somehow I received all text values of nodes with ns3 namespaces in one line. It was like this

SiteBO:15EBDS15EBDSAutomation testAfrica/Abidjan

But I can't reproduce the approach I used. Even though that is not what I am looking for)

Please help me to figure out where is the problem. Why I can't get the exact value of the node? What should I change?

You're using the wrong namespaceURI in your call to getElementsByTagNameNS - should be http://www.testsite.com/common :

public class Scratch2 {
    public static void main(String[] args) throws Exception {
        // @formatter:off

        String xml = "<soap-env:envelope xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap-env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + 
                "   <soap-env:body>\n" + 
                "      <ns4:findsiteconfigurationbysmth xmlns:ns3=\"http://www.testsite.com/common\" xmlns:ns2=\"http://www.testsite.com/plant\" xmlns:ns4=\"someapi:com:plant\" xmlns:ns5=\"someapi:com:reasoncode\">\n" + 
                "         <ns4:response>\n" + 
                "            <ns2:ref>SiteWD:QWERTY</ns2:ref>\n" + 
                "            <ns3:site>QWERTY</ns3:site>\n" + 
                "            <ns3:description>test description</ns3:description>\n" + 
                "            <ns3:timezone>Africa/Abidjan</ns3:timezone>\n" + 
                "         </ns4:response>\n" + 
                "      </ns4:findsiteconfigurationbysmth>\n" + 
                "   </soap-env:body>\n" + 
                "</soap-env:envelope>";

        // @formatter:on

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document myXml = builder.parse(new InputSource(new StringReader(xml)));

        // USING THE CORRECT namespaceURI BELOW
        NodeList nodeList = myXml.getElementsByTagNameNS("http://www.testsite.com/common", "site");

        System.out.println(nodeList.item(0)
                                   .getTextContent());

    }
}

Yields:

QWERTY

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