给出以下 SOAP 响应:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns3:signOnResponse xmlns:ns3="http://www.verimatrix.com/omi">
<sessionHandle>
<ns1:handle xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">h0PtwMXVmHp6Oqy7A6CmcrFrnVM=</ns1:handle>
</sessionHandle>
<result>
<ns1:resultId xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">admin</ns1:resultId>
<ns1:resultCode xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">0</ns1:resultCode>
<ns1:resultText xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">Success</ns1:resultText>
</result>
</ns3:signOnResponse>
</soapenv:Body>
</soapenv:Envelope>
如何获取位于 ns1:handle 的句柄 :h0PtwMXVmHp6Oqy7A6CmcrFrnVM=?
我的代码如下:
responseXml = responseXml.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "");
var response = new XML(responseXml);
// Determine the namespace of the SOAP Envelope:
//
var soap = response.namespace();
// Specify the namespace of the verify email response:
//
var ws = response.*.*.namespace();
// Set this namespace as a default to make parsing the response easier:
default xml namespace = ws
var responseBody = response.soap::Body.signOnResponse;
var handle = responseBody.signOnResponse.sessionHandle.handle
Alert(handle);
问题是它返回一个空值...
当我做 Alert(responseBody.toXMLString());
我得到:
<ns3:signOnResponse xmlns:ns3="http://www.verimatrix.com/omi" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<sessionHandle>
<ns1:handle xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">TBdn27dfFPlpWG/HTRgH16LsrkI=</ns1:handle>
</sessionHandle>
<result>
<ns1:resultId xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">admin</ns1:resultId>
<ns1:resultCode xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">0</ns1:resultCode>
<ns1:resultText xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">Success</ns1:resultText>
</result>
</ns3:signOnResponse>
我正在使用 Pentaho 数据集成并使用 javascript 步骤解析响应。
谢谢
最佳答案
如果您只对响应的这一特定部分感兴趣,那么让我们将响应本身视为一个字符串并捕获其中有趣的部分。
以下代码将为您完成:
var ns1Handle = responseXml.substring(responseXml.indexOf("<ns1:handle"));
ns1Handle = ns1Handle.substring(ns1Handle.indexOf(">")+1)
ns1Handle = ns1Handle.substring(0,ns1Handle.indexOf("<"))
检查 http://jsfiddle.net/Qz2ZN/ 处的工作脚本- 并查看您的 JavaScript 控制台。
:)
关于javascript - 如何用 javascript 解析以下 soap 响应? (不能使用 jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21034294/