我們有SOAP響應,我試圖解組或者只是從namesp1:result字段獲取值,但我總是得到null值,而在響應中值被設定爲D.SOAP字段總是作爲值返回爲空
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<namesp1:CheckTransactionDetailsResponse xmlns:namesp1="http://www.iesnare.com/dra/api/CheckTransactionDetails">
<namesp1:result xsi:type="xsd:string">D</namesp1:result>
<namesp1:reason xsi:type="xsd:string"/>
<namesp1:trackingnumber xsi:type="xsd:string">646498141355</namesp1:trackingnumber>
<namesp1:endblackbox xsi:type="xsd:string"/>
<namesp1:details>
<namesp1:detail>
<namesp1:name>device.cookie.enabled</namesp1:name>
<namesp1:value>1</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>device.flash.enabled</namesp1:name>
<namesp1:value>0</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>device.flash.installed</namesp1:name>
<namesp1:value>0</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>device.js.enabled</namesp1:name>
<namesp1:value>0</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>device.os</namesp1:name>
<namesp1:value>UNKNOWN</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>ipaddress</namesp1:name>
<namesp1:value>127.0.0.1</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>realipaddress</namesp1:name>
<namesp1:value>127.0.0.1</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>realipaddress.source</namesp1:name>
<namesp1:value>subscriber</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>ruleset.rulesmatched</namesp1:name>
<namesp1:value>0</namesp1:value>
</namesp1:detail>
<namesp1:detail>
<namesp1:name>ruleset.score</namesp1:name>
<namesp1:value>0</namesp1:value>
</namesp1:detail>
</namesp1:details>
</namesp1:CheckTransactionDetailsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我已經使用下面的代碼片斷:
獲取元件由tagnameNS,響應如下:
Name namesp1:result Value: null
使用Java代碼:
SOAPBody sb = soapResponse.getSOAPBody();
Document d = sb.extractContentAsDocument();
NodeList nodeList = d.getElementsByTagNameNS("http://www.iesnare.com/dra/api/CheckTransactionDetails", "result");
for (int i = 0; i < nodeList.getLength(); i++) {
org.w3c.dom.Node nodeElement = nodeList.item(i);
System.out.println("Name " + nodeElement.getNodeName());
System.out.println("Value: " + nodeElement.getNodeValue());
}
解組響應於對象,我認爲這是做到這一點的最佳方式。但是,我得到以下例外。
Error occurred while sending SOAP Request to Server java.lang.IllegalArgumentException: prefix xsd is not bound to a namespace
使用Java代碼:
SOAPBody sb = soapResponse.getSOAPBody();
Document d = sb.extractContentAsDocument();
DOMSource source = new DOMSource(d);
CheckTransactionDetailsResponse checkTransactionDetailsResponse = (CheckTransactionDetailsResponse) JAXB.unmarshal(source, CheckTransactionDetailsResponse.class);
System.out.println();
System.out.println();
System.out.println("Result: " + checkTransactionDetailsResponse.getResult());
System.out.println();
System.out.println();
有人可以幫助我找到正確的方式來獲得的迴應,我更喜歡使用方法2,但我看到了互聯網上的東西,這是JAXB中的一個錯誤。
感謝