我正在尝试阅读SOAP XML Request&的元素节点。使用下面的Java代码进行响应:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
NodeList nodeList = document.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println("Node Name: " + node.getNodeName() + ", Node Text: " + node.getNodeValue());
}
}
假设SOAP XML请求如下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:TestRequest xmlns:ns1="urn:wu:eh:dis:test:xsd:TestRequest-v1.0.0" xmlns:ns2="urn:wu:eh:dis:test:xsd:TestRequest" xmlns:ns3="urn:wu:eh:dis:test:xsd:TestSystem" xmlns:xyz1="urn:wu:eh:dis:test:xsd:TestSystem">
<ns2:TestRequest>
<ns3:TestSystem>
<ns3:UserName>test</ns3:UserName>
<ns3:Password>test</ns3:Password>
</ns3:TestSystem>
</ns2:TestRequest>
<xyz1:TestRequest>
<xyz1:TestSystem>
<xyz1:UserName>test</xyz1:UserName>
<xyz1:Password>test</xyz1:Password>
</xyz1:TestSystem>
</xyz1:TestRequest>
</ns1:TestRequest>
</soap:Body>
</soap:Envelope>
在上面的SOAP XML Request中,我能够使用命名空间ns2读取TestSystem模式的元素节点,而具有命名空间xyz1的TestSystem模式是不可读的。有人可以帮我解决Java代码中的更正/问题吗?
答案 0 :(得分:1)
构建SOAP xml是错误的,这就是为什么它没有正确解析。某些结束标记与开始标记不匹配:
<xyz1:UserName>test</ns3:UserName>
<xyz1:Password>test</ns3:Password>