首页 文章

使用JavaScript将SOAP / XML名称空间提取为键/值对的列表

提问于
浏览
0

这是一个SOAP响应,我想使用javascript来提取键值对的列表,其中键是本地名称空间元素前缀,例如:

SOAP-ENV
ns98
ns70

并且值是命名空间定义,例如:

http://schemas.xmlsoap.org/soap/envelope/
urn:vertexinc:enterprise:platform:security:messages:1:0
urn:vertexinc:enterprise:platform:security:1:0

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
     <ns98:ExecuteLoginResponse
        xmlns:ns70="urn:vertexinc:enterprise:platform:security:1:0"
        xmlns:ns98="urn:vertexinc:enterprise:platform:security:messages:1:0">
        <ns70:UserLoginConfirmation>
            <ns70:UserName>platformadmin</ns70:UserName>
            <ns70:LoginResult>SUCCESS</ns70:LoginResult>
            <ns70:LastLoginDate>2016-01-26T17:28:02.109</ns70:LastLoginDate>
            <ns70:DefaultAsOfDate>2016-01-26</ns70:DefaultAsOfDate>
            <ns70:ForcePasswordChange>false</ns70:ForcePasswordChange>
        </ns70:UserLoginConfirmation>
    </ns98:ExecuteLoginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

到目前为止,我的研究表明,通常不需要做这样的事情,大多数人要么将名称空间从他们关心的文档部分中删除,要么忽略命名空间 . 目前我正在剥离命名空间值,但出于调试目的,我希望检查响应中使用的命名空间 .

正在使用的服务的一个有趣方面是nsXX值可能因每次调用而异(例如,下次调用该方法时ns70可能是ns78) . 这意味着我无法键入固定的命名空间值 . 此外,WSDL中这些消息的命名空间值似乎与响应中的命名空间值没有任何对应关系 .

XPath似乎可以在这里提供帮助,或者我可以自己编写,但我更愿意利用现有的方法 . 在这一点上,我只是想了解问题空间,并希望得到关于从哪里开始我的教育过程的一些指导 . (请指出正确的方向)

1 回答

  • 0

    我采用的方法是fork angular-soap并添加构建响应命名空间列表的能力,因为XML响应被转换为对象树:

    SOAPClient._node2object = function (node, wsdlTypes, namespaces) {
        var namespaceURI = node.namespaceURI;
        var prefix = node.prefix;
    
        if ('undefined' != typeof namespaceURI) {
            namespaces[prefix] = namespaceURI;
        }
    
        ...
    }
    

    要获取名称空间列表,必须将 namespaces 对象传递给 $soap.post() 方法,如下所示 .

    var namespaces = new Object();
    ...
        $soap.post(webserviceurl, "SomeWebservice", parameters, namespaces).then(function (response) {
    ...
        }
    

    对angular-soap代码的更改需要修改 $soap.post(url, action, parameters, namespaces) 方法和 SOAPClient._node2object(node, wsdlTypes, namespaces) 函数之间的所有函数以传播 namespaces 参数 .

    http://www.w3schools.com/xml/dom_element.asp

相关问题