从请求中获取名称空间前缀,并在Spring Java中使用它查找元素

时间:2013-04-02 16:16:37

标签: java xml spring soap spring-ws

我想摆脱静态“ims:”并使用SOAP请求中找到的带有命名空间xmlns:ims="http://www.imsglobal.org/services/lis/bdems1p0/wsdl11/sync/imsbdems_v1p0的前缀。我怎么能在Spring拦截器中做到这一点?

protected String findProperty(SOAPHeader soapHeader, String propertyName) {

        NodeList list = soapHeader.getElementsByTagName("ims:" + propertyName);

我想要的属性是

<ims:imsx_syncRequestHeaderInfo>
         <ims:imsx_version>V1.0</ims:imsx_version>
         <ims:imsx_messageIdentifier>?</ims:imsx_messageIdentifier>
      </ims:imsx_syncRequestHeaderInfo>

1 个答案:

答案 0 :(得分:1)

修正了以下内容。

NodeList nodeList = soapHeader.getElementsByTagName("*");

for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE 
            && node.getLocalName().equalsIgnoreCase(propertyName)) {
            if (node.getTextContent().isEmpty()) {
                    return null;
            } else {
                    return node.getTextContent();
            }
    }
}