VBScript,MSXML和命名空间

前端之家收集整理的这篇文章主要介绍了VBScript,MSXML和命名空间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给出以下 XML

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetMsisdnResponse xmlns="http://my.domain.com/">
            <GetMsisdnResult>
                <RedirectUrl>http://my.domain.com/cw/DoIdentification.do2?sessionid=71de6551fc13e6625194</RedirectUrl>
            </GetMsisdnResult>
        </GetMsisdnResponse>
    </soap:Body>
</soap:Envelope>

我试图在VBScript中使用XPath访问RedirectUrl元素:

set xml = CreateObject("MSXML2.DOMDocument")
xml.async = false
xml.validateOnParse = false
xml.resolveExternals = false
xml.setProperty "SelectionLanguage","XPath"
xml.setProperty "SelectionNamespaces","xmlns:s='http://my.domain.com/' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"

err.clear
on error resume next
xml.loadXML (xmlhttp.responseText)
if (err.number = 0) then

    redirectUrl = xml.selectSingleNode("/soap:Envelope/soap:Body/s:GetMsisdnResponse/s:GetMsisdnResult/s:RedirectUrl").text
end if

但它找不到RedirectUrl节点,因此当我尝试获取.text属性时没有任何内容.我究竟做错了什么

解决方法

您正在使用错误的命名空间声明.

在您的XML中

http://www.w3.org/2003/05/soap-envelope

但在你的脚本中,你使用

http://schemas.xmlsoap.org/soap/envelope/

这对我有用:

xml.setProperty "SelectionNamespaces","xmlns:s='http://my.domain.com/' xmlns:soap='http://www.w3.org/2003/05/soap-envelope'"

' ...

Set redirectUrl = xml.selectSingleNode("/soap:Envelope/soap:Body/s:GetMsisdnResponse/s:GetMsisdnResult/s:RedirectUrl")

另一方面 – 我会尝试将受On Error Resume Next语句影响的行保持在绝对最小值.理想情况下,它仅适用于单个临界线(或者您将临界区包裹在Sub中).这使调试变得更容易.

例如,您在Set redirectUrl = ….中缺少Set语句.当On Error Resume Next打开时,这将无提示失败.

尝试

' this is better than loadXML(xmlHttp.responseText)
xmlDocument.load(xmlHttp.responseStream)

If (xmlDocument.parseError.errorCode <> 0) Then
  ' react to the parsing error
End If

Xpath = "/soap:Envelope/soap:Body/s:GetMsisdnResponse/s:GetMsisdnResult/s:RedirectUrl"
Set redirectUrl = xml.selectSingleNode(Xpath)

If redirectUrl Is Nothing Then
  ' nothing found
Else
  ' do something
End If

请参阅 – 否错误恢复下一步必要.

猜你在找的XML相关文章