【问题标题】:Can't process SOAP response无法处理 SOAP 响应
【发布时间】:2013-05-05 22:54:53
【问题描述】:

我有以下代码:

SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
          SOAPConnection connection = sfc.createConnection();

          MessageFactory mf = MessageFactory.newInstance();
          SOAPMessage sm = mf.createMessage();
          SOAPHeader sh = sm.getSOAPHeader();
          SOAPBody sb = sm.getSOAPBody();
          sh.detachNode();
          MimeHeaders mimeHeader = sm.getMimeHeaders();

          //change header's attribute
          mimeHeader.setHeader("SOAPAction", "\"urn:Belkin:service:basicevent:1#GetBinaryState\"");
          //sh.addAttribute(SOAPFactory.newInstance().createName("SOAPAction", "", "urn:Belkin:service:basicevent:1#SetBinaryState"),"");
          QName bodyName = new QName("urn:Belkin:service:basicevent:1", "GetBinaryState", "u");

          //sm.add            

          SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
          QName qn = new QName("BinaryState");

          System.out.println("\n Soap Request:\n");
          sm.writeTo(System.out);
          System.out.println();

          URL endpoint = new URL("http://" + ipAddress + ":49153/upnp/control/basicevent1");
          SOAPMessage response = connection.call(sm, endpoint);

          connection.close();
          System.out.println(response.getContentDescription());
        } catch (Exception ex) {
          ex.printStackTrace();
        }

给我这个回复(在wireshark中)

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"                    
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <s:Body>
        <u:GetBinaryStateResponse xmlns:u="urn:Belkin:service:basicevent:1">
            <BinaryState>0</BinaryState>
        </u:GetBinaryStateResponse>
    </s:Body>
</s:Envelope>

我想在 Java 中获取 BinaryState 值 (0) 或 (1)。 我很难弄清楚它在SOAPMessage 响应中的位置。帮助会很棒。或者即使我可以将 XML 响应放在一个字符串中并自己解析它。

【问题讨论】:

    标签: java parsing soap response


    【解决方案1】:

    将 SOAPMessage 转换为字符串,你有

    SOAPMessage response = connection.call(sm, endpoint);
    

    这样做:

    SOAPMessage response = connection.call(sm, endpoint);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    response.writeTo(os);
    String responseXml = new String(os.toByteArray());
    // work with responseXml here...
    

    编辑:

    作为替代方案,如果您只想要一个您已经知道名称的元素的特定值(并且至少存在一个),您可以通过以下方式获得它:

    String responseBinaryState = response.getSOAPBody()
                                         .getElementsByTagName("BinaryState")
                                         .item(0).getTextContent();
    System.out.println("BinaryState: "+responseBinaryState);
    

    【讨论】:

    • 漂亮!非常感谢。一直在拉我的头发,谷歌没有帮助。
    • XML已经被解析了,为什么建议把它转回String呢?
    • @flup 你说得对,我根据他提出问题的可能性给出了String 选项。我将添加一个替代方案而不变成String
    【解决方案2】:

    response.getSOAPBody() 会给你一个主体的 Dom 节点,获取它的子节点与导航正常的 Dom 树没有任何不同。

    因此您可以在树中导航(未经测试,进行调试!):

    Node body = response.getSOAPBody();
    Node binStateResponse = body.getFirstChild();
    Node binaryState = binStateResponse.getFirstChild();
    Node binaryStateText = binaryState.getFirstChild();
    String result = binaryStateText.getNodeValue();
    

    或者使用 XPath 表达式更可靠地找到答案,与正文的确切内容无关。

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    Node body = response.getSOAPBody();
    String result = xpath.evaluate(
        "//BinaryState/text()", 
        body);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 2015-12-19
      • 2011-02-25
      • 2018-12-17
      • 1970-01-01
      相关资源
      最近更新 更多