2013-05-05 127 views
1

我有以下代碼:無法處理SOAP響應

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> 

我想要得到的BinaryState值(0)或(1 )在Java中。 我很難找出它在SOAPMessage響應中的位置。幫助會很好。或者即使我可以將XML響應放入一個字符串中並自己解析它。

回答

2

要轉的SOAPMessage爲String,那就是你有

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

美麗!非常感謝。一直拉着我的頭髮,谷歌沒有幫助。 – Fidel 2013-05-05 23:08:14

+0

XML已被解析,爲什麼建議將其重新轉換爲String? – flup 2013-05-05 23:15:21

+0

@flup你說得對,我根據他提出的問題給出了'String'選項。我將添加一個替代方案,而不用轉換成String。 – acdcjunior 2013-05-05 23:31:30

0

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