好的,所以我得到以下作为肥皂响应:

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


我正在尝试通过这段代码获取<a:lastInvoiceDate></a:lastInvoiceDate>的值:

SOAPBody sBody = response.getSOAPBody();
QName gcdbdbr = new QName("http://services.domain.com/SelfCare", "GetCustomerDetailsByDeviceNumberResponse");
java.util.Iterator iterator = sBody.getChildElements(gcdbdbr);
while(iterator.hasNext()){
      NodeList nodeList = sBody.getElementsByTagName("lastInvoiceDate");
      Element element = (Element) nodeList.item(0);
      Node child = element.getFirstChild();
      String data = child.getTextContent();
      System.out.println(data);
}


但它是空的。

如何获得<a:lastInvoiceDate>的值?

最佳答案

您的代码看起来不错,但是当您使用getElementsByTagName()时,还必须在字符串参数中包括名称空间,如下所示:

...
NodeList nodeList = sBody.getElementsByTagName("a:lastInvoiceDate");
...


如果要在查找中省略名称空间,则可以选择使用函数getElementsByTagNameNS()和通配符'*'作为第一个参数,将节点名用作第二个参数,如下所示:

NodeList nodeList = sBody.getElementsByTagNameNS("*", "lastInvoiceDate");

10-08 17:06