【问题标题】:get an element deep within a soap response在肥皂响应中获得一个元素
【发布时间】:2016-05-08 12:57:58
【问题描述】:

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

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

我正在尝试使用这段代码获取&lt;a:lastInvoiceDate&gt;&lt;/a:lastInvoiceDate&gt; 的值:

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

但它是空的。

如何获得&lt;a:lastInvoiceDate&gt; 的值?

【问题讨论】:

    标签: java soa saaj


    【解决方案1】:

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

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

    如果您想在查找中省略命名空间,您也可以使用函数getElementsByTagNameNS(),将通配符'*' 作为第一个参数,将您的节点名作为第二个参数,如下所示:

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

    【讨论】:

      【解决方案2】:

      你不需要迭代

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

      【讨论】:

        猜你喜欢
        • 2016-09-10
        • 1970-01-01
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 2014-09-05
        • 2019-04-23
        • 1970-01-01
        • 2015-01-21
        相关资源
        最近更新 更多