Java Soap 请求 - 读取soap响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20460870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Soap request - reading soap response
提问by J33nn
I'm trying to get specific values from response I get from webservice. Unfortunately I don't know how to do it. I used code found on stackoverflow for creating soap request and writing out response content into stdout:
我正在尝试从我从网络服务获得的响应中获取特定值。不幸的是,我不知道该怎么做。我使用在 stackoverflow 上找到的代码来创建soap请求并将响应内容写入标准输出:
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);
}
It all works well but I dont need whole response content:
一切正常,但我不需要完整的响应内容:
<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 value of code and description from this response. How can I do this?
我只需要此响应中的代码和描述值。我怎样才能做到这一点?
回答by newuserua
Here is the whole working example for some other xml example;
这是其他一些 xml 示例的整个工作示例;
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());
}
}
}
}
and imports if you needed ;
如果需要,可以导入;
- 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;
- 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;