简体   繁体   English

SOAP RPC 响应解析

[英]SOAP RPC response parsing

Hi I am working on an application which is consuming SOAP RPC service.您好,我正在开发一个使用 SOAP RPC 服务的应用程序。 I have created a client code and getting soapmessage response from SOAP service.我已经创建了一个客户端代码并从 SOAP 服务获得了 soapmessage 响应。 SOAP services are designed in old technologies and not supporting JAXB or AXIS2. SOAP 服务采用旧技术设计,不支持 JAXB 或 AXIS2。

My SOAPMessage response is like:我的 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>

Now I have to get the value of "SOAP_SERVICE" from the map and store it into some variable.现在我必须从地图中获取“SOAP_SERVICE”的值并将其存储到某个变量中。 Can someone please suggest me something how I can proceed.有人可以建议我如何继续。

Thanks.谢谢。

Adding the client code.添加客户端代码。

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

Ok, so this might look ugly, but as you cannot use another technology, you will have to traverse the SOAP response just as a DOM object.好的,所以这可能看起来很难看,但是由于您不能使用其他技术,您将不得不像 DOM 对象一样遍历 SOAP 响应。

This means, first locate the item nodes.这意味着,首先定位item节点。 Then, go through them one by one, searching for the item that has a child key node with value SOAP_SERVICE .然后,经过逐一,搜索item有子key与值节点SOAP_SERVICE

Once found, go through key node children, until you find the value node.找到后,遍历key节点子节点,直到找到value节点。

Here is a working example (continued from your code)这是一个工作示例(从您的代码继续)

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

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM