Java Soap请求 – 读取soap响应

我正在尝试从webservice获取响应中的具体值.不幸的是我不知道该怎么做我使用stackoverflow上找到的代码来创建soap请求并将响应内容写入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);
}

这一切都很好,但我不需要全面的回应内容

<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>

我只需要这个响应的代码和描述的价值.我该怎么做?

解决方法

以下是其他一些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());
            }
        }
    }

}

如果需要,进口;

> 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;

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...