SOAP RPC响应解析

时间:2017-01-16 12:43:07

标签: java soap xml-parsing

您好我正在使用一个正在使用SOAP RPC服务的应用程序。我创建了一个客户端代码并从SOAP服务获取soapmessage响应。 SOAP服务采用旧技术设计,不支持JAXB或AXIS2。

我的SOAPMessage响应如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:LoginResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
<LoginReturn xsi:type="ns1:ResultMap">
  <items xsi:type="soapenc:Array" soapenc:arrayType="ns1:ResultMapItem[2]" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
 <item>
  <key xsi:type="xsd:string">SOAP_SERVICE</key>
  <value xsi:type="xsd:string">https://somevalue</value>
 </item>
 <item>
  <key xsi:type="xsd:string">UI_SERVICE</key>
  <value xsi:type="xsd:string">https://somevalue</value>
 </item>
</items>
</LoginReturn>
</ns1:LoginResponse>
</soapenv:Body>
</soapenv:Envelope>

现在我必须得到&#34; SOAP_SERVICE&#34;的价值。从地图中将其存储到一些变量中。 有人可以建议我如何继续。

感谢。

添加客户端代码。

        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
       String url = "https://serviceUrl"; 
       String soapRequest = "requeststring";

       System.out.println("soapRequest.."+soapRequest);

      InputStream is = new ByteArrayInputStream(soapRequest.getBytes());
      SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);
      SOAPMessage soapResponse = soapConnection.call(request, url);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      soapResponse.writeTo(out);

1 个答案:

答案 0 :(得分:0)

好的,所以这可能看起来很难看,但是由于你不能使用其他技术,你必须像DOM对象一样遍历SOAP响应。

这意味着,首先找到item个节点。然后,逐个浏览它们,搜索具有值为item的子key节点的SOAP_SERVICE

找到后,请浏览key个节点子节点,直到找到value节点。

这是一个工作示例(从您的代码继续)

    SOAPBody body = soapResponse.getSOAPBody();

    NodeList returnList = body.getElementsByTagName("item");

    for (int i = 0; i < returnList.getLength(); i++) {
        NodeList innerResultList = returnList.item(i).getChildNodes();

        for (int j = 0; j < innerResultList.getLength(); j++) {
            if ("KEY".equalsIgnoreCase(innerResultList.item(j).getNodeName()) 
                    && "SOAP_SERVICE".equalsIgnoreCase(innerResultList.item(j).getTextContent())) {
                for (int k = j +1; k < innerResultList.getLength(); k++){
                    if ("VALUE".equalsIgnoreCase(innerResultList.item(k).getNodeName())) {
                        System.out.println("Value for SOAP_SERVICE is " + innerResultList.item(k).getTextContent());
                        break;
                    }
                }
            }
        }
    }

希望这有帮助

相关问题