Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取soap响应中的深层元素_Java_Soa_Saaj - Fatal编程技术网

Java 获取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

好的,我得到以下soap响应:

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