Java soap request – read soap response
•
Java
I am trying to get the specific value in the response from WebService Unfortunately, I don't know what to do. I use the code found on stackoverflow to create a soap request and write the response content to stdout:
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent,result);
}
All this is good, but I don't need a comprehensive response:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bin="http://localhost/WebService/bindings" xmlns:typ="http://localhost/WebService/types">
<soapenv:Header/>
<soapenv:Body>
<bin:doActionResponse>
<bin:out>
<typ:result>
<typ:code>?</typ:code>
<typ:description>?</typ:description>
</typ:result>
</bin:out>
</bin:doActionResponse>
</soapenv:Body>
</soapenv:Envelope>
I just need the code and description value of this response What shall I do?
Solution
The following is an overall working example of some other XML examples;
public static void main(String[] args) throws IOException,SOAPException {
String xmlInput = " <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://litwinconsulting.com/webservices/\">\n"
+ " <soapenv:Header/>\n"
+ " <soapenv:Body>\n"
+ " <web:RES>\n"
+ " <web:RETURNCODE>100 </web:RETURNCODE> \n"
+ " </web:RES>\n"
+ " <web:GetWeather>\n"
+ " <!--Optional:-->\n"
+ " <web:City>%CITY%</web:City>\n"
+ " </web:GetWeather>\n"
+ " </soapenv:Body>\n"
+ " </soapenv:Envelope>";
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage(
new MimeHeaders(),new ByteArrayInputStream(xmlInput.getBytes(Charset
.forName("UTF-8"))));
SOAPBody body = message.getSOAPBody();
NodeList returnList = body.getElementsByTagName("web:RES");
boolean isSuccess = false;
for (int k = 0; k < returnList.getLength(); k++) {
NodeList innerResultList = returnList.item(k).getChildNodes();
for (int l = 0; l < innerResultList.getLength(); L++) {
if (innerResultList.item(l).getNodeName()
.equalsIgnoreCase("web:RETURNCODE")) {
isSuccess = Integer.valueOf(innerResultList.item(l)
.getTextContent().trim()) == 100 ? true : false;
}
}
}
if (isSuccess) {
NodeList list = body.getElementsByTagName("web:GetWeather");
for (int i = 0; i < list.getLength(); i++) {
NodeList innerList = list.item(i).getChildNodes();
for (int j = 0; j < innerList.getLength(); j++) {
System.out.println(innerList.item(j).getNodeName());
System.out.println(innerList.item(j).getTextContent());
}
}
}
}
If necessary, import;
> java. io. ByteArrayInputStream;> java. io. IOException;> java. nio. charset. Charset;> javax. xml. soap. MessageFactory;> javax. xml. soap. MimeHeaders;> javax. xml. soap. soAPBody;> javax. xml. soap. soAPException;> javax. xml. soap. soAPMessage;> org. w3c. dom. NodeList;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
