在VBScript中遍历和过滤xml节点 [英] Traverse and filter xml nodes in VBScript

查看:28
本文介绍了在VBScript中遍历和过滤xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从以下 xml 中获取每个客户的公司、名字、姓氏、手机、电子邮件、国家/地区、城市和邮政编码值?我尝试了以下代码,但是当缺少某些值(例如第一条记录中的电子邮件)时,代码会从下一条记录分配值,因此ABCars"获得gwood@gmail.com".

how can I get company, firstname, lastname, mobile, email, country, city and zipcode values for each customer from the below xml? I tried the following code but when some value is missing (e.g. email in the first record) the code assigns value from the next record so "ABCars" gets "gwood@gmail.com".

另外,你知道我如何过滤加载的记录,以便只留下另一个记录集中不存在的手机号码记录(记录集不包含在下面的代码中)?

Also, do you know how can I filter the loaded records so that only records with mobile number not existing in another recordset left (recordset is not included in the below code)?

Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.async="false"
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load("test.xml")

set  nodes=xmlDoc.SelectNodes("//customer")
for i=0 to nodes.length-1
set company=xmlDoc.SelectNodes("//customer/company")
set firstname=xmlDoc.SelectNodes("//customer/firstname")
set lastname=xmlDoc.SelectNodes("//customer/lastname")
set mobile=xmlDoc.SelectNodes("//customer/mobile")
set email=xmlDoc.SelectNodes("//customer/email")
set country=xmlDoc.SelectNodes("//customer/address/country")
set city=xmlDoc.SelectNodes("//customer/address/city")
set zipcode=xmlDoc.SelectNodes("//customer/address/zipcode")

XML:

<?xml version="1.0" encoding="UTF-8"?>
<list>
    <category>Cars</category>
    <customers>
        <customer>
            <company>ABCars</company>
            <firstname>Peter</firstname>
            <lastname>Heinrich</lastname>
            <mobile>9141453027</mobile>
            <address>
                <country>Germany</country>
                <city>Berlin</city>
                <zipcode>12345</zipcode>
            </address>
        </customer>
        <customer>
            <company>Best Cars</company>
            <firstname>George</firstname>
            <lastname>Wood</lastname>
            <mobile>123456789</mobile>
            <email>gwood@gmail.com</email>
            <address>
                <country>Great Britain</country>
                <city>Leicaster</city>
                <zipcode>67890</zipcode>
            </address>
        </customer>
    </customers>
</list>

问候,普热美克

推荐答案

您的循环遍历 nodes 集合,但循环体每次迭代都会查询整个文档,因此您每次都执行相同的查询time - 使用返回的节点作为上下文节点来选择与该节点相关的节点:

Your loop goes through nodes collection but the loop body queries the whole document for each iteration, so you are doing the same query every time - use the returned nodes as context nodes to select nodes relative to that node:

set  nodes=xmlDoc.SelectNodes("//customer")
for i=0 to nodes.length-1
    ' get the current node
    set node = nodes(i)
    ' run xpath relative to the current node
    set company = node.selectSingleNode("company")
    set firstname = node.selectSingleNode("firstname")
    set lastname = node.selectSingleNode("lastname")
    set mobile = node.selectSingleNode("mobile")
    set email = node.selectSingleNode("email")
    set country = node.selectSingleNode("address/country")
    set city = node.selectSingleNode("address/city")
    set zipcode = node.selectSingleNode("address/zipcode")

next

如果 XPath 查询未找到任何节点,将返回 Nothing,您可以对其进行适当的检查和路由:

If there is no node found by the XPath query, Nothing will be returned which you can check and route appropriately:

set company = node.selectSingleNode("company")
if company is Nothing then
    ' do something if no company, eg break loop
    exit for
end if

对于过滤,您可以构建 XPath 查询以仅选择不在该列表中的那些节点,例如

For the filtering, you could build your XPath query to select only those nodes which aren't in that list, eg

dim xpath: xpath = Empty

do until recordset.EOF
    if xpath <> "" then
       xpath = xpath & " and "
    end if 
    xpath = xpath & "mobile != '" & recordset("mobile") & "'"

    recordset.MoveNext
loop

if xpath <> "" then
    xpath = "[" & xpath & "]"
end if

' eg you end up with something like "//customer[mobile != 123456789 and mobile != 987654321]" 
set nodes = xmldoc.selectNodes("//customer" & xpath)

这篇关于在VBScript中遍历和过滤xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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