我试图从我从webservice得到的响应中获取特定值。不幸的是我不知道该怎么做。我用计算器发现代码创建SOAP请求,并写出响应内容到标准输出:Java肥皂请求 - 阅读肥皂响应
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);
}
这一切运作良好,但我不需要整个响应内容:
<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>
我只是需要的代码值以及这个回应的描述。我怎样才能做到这一点?
看看XPATH如果你试图从肥皂消息中获取单个元素http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java –
我相信有一种方法可以做这样的事情:'soapResponse.getSOAPBody()。getElementsByTagName()',但我一直在尝试这样做时会遇到一些奇怪的值。 – J33nn