VBScript 无法选择 XML 节点 [英] VBScript Can not Select XML nodes

查看:13
本文介绍了VBScript 无法选择 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一些 web 服务响应 XML 中选择节点但无济于事.出于某种原因,我可以选择根节点(xmldata")但是,当我尝试更深入地钻取(xmldata/customers")时,一切都返回为空!以下是 Web 服务返回的 XML 示例.

I am trying to Select nodes from some webservice response XML to no avail. For some reason I am able to select the root node ("xmldata") however, when I try to drill deeper("xmldata/customers") everything is returned empty! Below is the a sample of the XML that is returned by the webservice.

<xmldata>
  <customers>
    <customerid>22506</customerid>
    <firstname>Jim</firstname>
    <issuperadmin>N</issuperadmin>   
    <lastname>Jones</lastname>
  </customers>
</xmldata>

这是我尝试选择 customerid、firstname 和 lastname 的代码;

and here is the code I am trying to select customerid, firstname, and lastname;

' Send the Xml
oXMLHttp.send Xml_to_Send

' Validate the Xml
dim xmlDoc
set xmlDoc = Server.CreateObject("Msxml2.DOMDocument")
xmlDoc.load (oXMLHttp.ResponseXML.text)
if(len(xmlDoc.text) = 0) then
    Xml_Returned = "<B>ERROR in Response xml:<BR>ERROR DETAILS:</B><BR><HR><BR>" 
end if

dim nodeList
Set nodeList = xmlDoc.SelectNodes("xmldata/customers")

For Each itemAttrib In nodeList
    dim custID, custLname, custFname    
    custID =itemAttrib.selectSingleNode("customerid").text
    custLname =itemAttrib.selectSingleNode("lastname").text
    custFname =itemAttrib.selectSingleNode("firstname").text
    response.write("News Subject: " & custID)
    response.write("<br />News Subject: " & custLname)
    response.write("<br />News Date: " & custFname)
Next

上面代码的结果是zilch!页面上没有写入任何内容.一件奇怪的事情是,如果我选择根元素并按如下方式获取其长度;

The result of the code above is zilch! nothing is written to the page. One strange thing is if I select the root element and get its length as follows;

Set nodeList = xmlDoc.SelectNodes("xmldata")
Response.Write(nodeList.length) '1 is written to page

它正确地确定了 1 的长度.但是,当我尝试对下一个节点进行相同操作时,如下所示;

It correctly determines the length of 1. However when I try the same with the next node down as follows;

Set nodeList2 = xmlDoc.SelectNodes("xmldata/customers")
Response.Write(nodeList.length) '0 is written to page

它返回长度为 0.为什么!

It returns a length of 0. WHY!

请注意,这不是我尝试访问这些节点值的唯一方法.我只是无法弄清楚我做错了什么.有人可以帮我吗.干杯.

Please note that this isn't the only way I have attempted to access the values of these nodes. I just can not work out what I am doing wrong. Could someone please help me out. Cheers.

推荐答案

好的,所以我终于弄清楚了我做错了什么.由于我正在检索的 xml 来自 web 服务,并且我对它的信息有限,因此我使用以下内容将 xml 写入页面,以便查看它的结构.

Ok, so I finally worked out what I was doing wrong. As the xml I was retrieving was from a webservice, and I had limited information about it, I used the following to write the xml to the page so that I could see how it was structured.

Response.Write oXMLHttp.ResponseXml.xml

出于某种原因(也许有人可以填写这部分),它以小写形式编写了所有 XML 标记.事实证明,经过一番调查和以下操作后,我发现这不是事实!

For some reason(maybe someone could fill this part in) it wrote all the XML tags in lower case. It turns out that after some investigation and after doing the following I found out that this was not the truth!

dim nodeList
Set nodeList = xmlDoc.SelectNodes("//xmldata/")

for each item In nodeList
    response.write(item.text & " -> Tag Name: " & item.nodeName & "<br />")
Next

'this wrote the following to the page
'22506 -> Tag Name: CustomerID
'Jim -> Tag Name: FirstName
'N -> Tag Name: IsSuperAdmin
'Jones 2 -> Tag Name: LastName

如您所见,nodeName"属性输出显示标签为驼峰式.所以 ResponseXML 相当具有误导性,并且看到 XPath 区分大小写使我无法选择有问题的节点.

As you can see the 'nodeName' property output shows the tags to be camel case. So that ResponseXML was rather misleading and seeing XPath is case sensitive was preventing me from selecting the Nodes in question.

这篇关于VBScript 无法选择 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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