我有以下代碼:無法處理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響應放入一個字符串中並自己解析它。
美麗!非常感謝。一直拉着我的頭髮,谷歌沒有幫助。 – Fidel 2013-05-05 23:08:14
XML已被解析,爲什麼建議將其重新轉換爲String? – flup 2013-05-05 23:15:21
@flup你說得對,我根據他提出的問題給出了'String'選項。我將添加一個替代方案,而不用轉換成String。 – acdcjunior 2013-05-05 23:31:30