0

Have this soap message

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <BRANCH xmlns="https://ya.yandex.ru/get-instance">
         <mtu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
         <header>
            <MANE>032036</MANE>
            <TYPE>GET</TYPE>
         </header>
         <body>
            <BR_CODE>666</BR_CODE>
            <NAME>hello</NAME>
             <ADDRESS>USA</ADDRESS>
            <IS_ACTIVE>true</IS_ACTIVE>
         </body>
      </BRANCH>
   </soapenv:Body>
</soapenv:Envelope>

I want to get only this. With tags:

         <BR_CODE>666</BR_CODE>
        <NAME>hello</NAME>
         <ADDRESS>USA</ADDRESS>
        <IS_ACTIVE>true</IS_ACTIVE>

But i have this in output :

666 hello USA true

There is my code

      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse((new InputSource(new StringReader(response))));
    doc.getDocumentElement().normalize();
    NodeList list = doc.getElementsByTagName("body");
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        System.out.println(node.getTextContent());
    }

Whats wrong in my code? Can not get what i want.

1 Answers1

0

Mitchell, you have your NodeList from doc.getElementsByTagName("body"). First, you create a new org.w3c.dom.Document newXmlDoc where you store the nodes from your NodeList; you create a new root element, and append it to newXmlDoc; then, for each node n in your NodeList, you import n into newXmlDoc, and then you append n as a child of root. See the code in answer 1 here:

https://stackoverflow.com/a/5787450/6655604

D. Spreuer
  • 47
  • 8