我想知道当子标记看起来像这样时,如何获取SOAP响应体子的内容:
<sth:x>...</sth:x>
在Oracle文档中,我找到了loop all Elements with a specific name的方法。 但是因此我需要首先创建一个Element,它指定我想要搜索的标签的名称。
如何创建一个与上面相似的元素?我知道怎么做这样的:
<sth:id sth="asdf">
但这并没有真正起作用。 这是我试图阅读的服务器响应。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<ns5:loginResponse xmlns:ns5="x">
<ns5:id>Value I am looking for</ns5:id>
<ns5:rc>0</ns5:rc>
</ns5:loginResponse>
</soapenv:Body>
</soapenv:Envelope>
感谢您的帮助:)
答案 0 :(得分:1)
试试这个。
String xml = "YourXMl" DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS("x","id");
System.err.println(nodes.item(0).getChildNodes().item(0).getNodeValue());
有两个重要的事情factory.setNamespaceAware(true);
- 打开对xml命名空间的支持。
doc.getDocumentElement().getElementsByTagNameNS("x","id");
使用命名空间uri和元素名称获取元素。这里是名称空间uri的<ns5:loginResponse xmlns:ns5="x">
声明。 x
是uri ns5
是命名空间。
答案 1 :(得分:0)
我找到了答案:
有问题的元素在另一个SOAPBodyElement中。 因此循环遍历这两个元素给了我正确的价值:
body = reply.getSOAPBody();
Iterator replyIt = body.getChildElements();
while(replyIt.hasNext()){
SOAPBodyElement replysbe = (SOAPBodyElement) replyIt.next();
Iterator replyIt2 = replysbe.getChildElements();
SOAPElement sentSE = (SOAPElement) replyIt2.next();
sessionID = sentSE.getValue();
}