有这条肥皂消息
<?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>
我想要得到这个。随着标签:
<BR_CODE>666</BR_CODE>
<NAME>hello</NAME>
<ADDRESS>USA</ADDRESS>
<IS_ACTIVE>true</IS_ACTIVE>
但我在输出中有这个:
666 hello USA true
有我的代码
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());
}
我的代码有什么问题?无法得到我想要的东西。
答案 0 :(得分:0)
Mitchell,你有来自doc.getElementsByTagName的NodeList(&#34; body&#34;)。首先,创建一个新的org.w3c.dom.Document newXmlDoc,在其中存储NodeList中的节点;你创建一个新的根元素,并将其附加到newXmlDoc;然后,对于NodeList中的每个节点n,将n导入newXmlDoc,然后将n附加为root的子节点。请参阅答案1中的代码:
stackoverflow.com/questions/5786936 /