0

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?

1

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Excuse me. My bad. I did mistake while adding XML into the question.Just edited the question. Now it's correct. Question still actual
@RomanShabanov I think my answer still provides the solution, no? It outputs "QWERTY". Is there something missing in this solution?
you are my hero! Many thanks! You really saved me! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.