Reputation: 243
I am trying to read the element nodes of the SOAP XML Request & Response using the below piece of Java code:
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());
}
}
Let say the SOAP XML Request is as below:
<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>
In the above SOAP XML Request, I am able to read the element nodes of TestSystem schema with the namespace ns2 whereas the TestSystem schema with namespace xyz1 is not readable. Could anyone please help me the correction/problem in the Java code?
Upvotes: 0
Views: 65
Reputation: 951
The construction of your SOAP xml is wrong, that is why it is not gettting parsed properly. Some of the end tags are not matching the start tags:
<xyz1:UserName>test</ns3:UserName>
<xyz1:Password>test</ns3:Password>
Upvotes: 1