VBScript、MSXML 和命名空间 [英] VBScript, MSXML and Namespaces

查看:62
本文介绍了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 元素:

I am trying to access the RedirectUrl element using XPath in VBScript:

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 属性时什么也没有.我做错了什么

but it is failing to find the RedirectUrl node, therefore is nothing when I try to get the .text property. What am I doing wrong

推荐答案

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

You are using the wrong namespace declaration.

在您的 XML 中,您有

In your XML you have

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

但在您的脚本中,您使用

but in your Script, you use

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 语句影响的行保持在 absolute 最低限度.理想情况下,它仅对单个关键行有效(或者您将关键部分包装在 Sub 中).这使得调试很多更容易.


On a different note - I'd try to keep the lines that are affected by an On Error Resume Next statement at an absolute minimum. Ideally, it is in effect for a single critical line only (or you wrap the critical section in a Sub). This makes debugging a lot easier.

例如,您在 Set redirectUrl = ... 中缺少 Set 语句.当 On Error Resume Next 开启时,这将静默失败.

For example, you are missing a Set statement in Set redirectUrl = .... This will fail silently when On Error Resume Next is on.

试试

' 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

参见 - 不需要 On Error Resume Next.

这篇关于VBScript、MSXML 和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆