我通过AJAX调用从基于SOAP的Web服务接收以下XML。使用XPath在JavaScript中解析(基于SOAP)XML
<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId
CorrelationId="d2ba6844-ec96-4ddc-a3aa-36c8bdd2daeb"
xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">
8c74da56-65c1-4f50-9dc6-547dc9b208a6
</ActivityId>
</s:Header>
<s:Body>
<MyResponse
xmlns="http://foo.example.com/">
<MyResult
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Bar>
1ab6cd3a6499
</Bar>
</MyResult>
</MyResponse>
</s:Body>
</s:Envelope>
我想使用XPath的(基于JavaScript的)客户端解析XML找到Bar
值。
我能用XPath在iOS上用KissXML解析这个XML。我首先将名为baz
的名称空间添加到值为"http://foo.example.com/"
的根元素,然后我可以用类似"/s:Envelope/s:Body/baz:MyResponse/baz:MyResult/baz:Bar"
的XPath表达式调用nodesForXPath:error:
。
尽管读取MDN上的Introduction to using XPath in JavaScript,我无法弄清楚如何正确调用evaluate
函数。
如何在JavaScript中解析此XML?
(我没有任何外部库的任何相关性,比如jQuery的,所以我想,如果我的解决方案并不需要我来添加任何图书馆喜欢。)
那是什么用加入了“巴兹”命名空间?您不必为此创建名称空间声明。 –
添加'baz'命名空间允许我在我的XPath语句中为'MyResponse','MyResult'和'Bar'添加前缀,因为它们都在MyResponse元素中声明的默认名称空间中。 – user2719740