Java 获取soap响应中的深层元素
好的,我得到以下soap响应:Java 获取soap响应中的深层元素,java,soa,saaj,Java,Soa,Saaj,好的,我得到以下soap响应: <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <GetCustomerDetailsByDeviceNumberResponse xmlns="http://services.domain.com/SelfCare"> <GetCustomerDetailsByDeviceN
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetCustomerDetailsByDeviceNumberResponse xmlns="http://services.domain.com/SelfCare">
<GetCustomerDetailsByDeviceNumberResult xmlns:a="http://datacontracts.domain.com/SelfCare"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:AuditReferenceNumber i:nil="true"/>
<a:accounts>
<a:Account>
<a:lastInvoiceAmount>0</a:lastInvoiceAmount>
<a:lastInvoiceDate>0001-01-01T00:00:00</a:lastInvoiceDate>
</a:Account>
</a:accounts>
</GetCustomerDetailsByDeviceNumberResult>
</GetCustomerDetailsByDeviceNumberResponse>
</s:Body>
</s:Envelope>
但它是空的
如何获取
的值?您的代码似乎很好,但是当您使用getElementsByTagName()
时,还必须在字符串参数中包含名称空间,如下所示:
...
NodeList nodeList = sBody.getElementsByTagName("a:lastInvoiceDate");
...
NodeList nodeList = sBody.getElementsByTagNameNS("*", "lastInvoiceDate");
如果要在查找中省略名称空间,也可以使用函数GetElementsByTagnames()
,将通配符“*”
作为第一个参数,将节点名称作为第二个参数,如下所示:
...
NodeList nodeList = sBody.getElementsByTagName("a:lastInvoiceDate");
...
NodeList nodeList = sBody.getElementsByTagNameNS("*", "lastInvoiceDate");
你不需要迭代
SOAPBody sBody = response.getSOAPBody();
NodeList nodeList = sBody.getElementsByTagName("lastInvoiceDate");
// Here you only need to loop nodeList if you have multiple elements with the same tag name
System.out.println(nodeList.item(0).getFirstChild().getTextContent());