VBScript 遍历 XML 子节点并检索值 [英] VBScript iterating through XML child nodes and retrieving values

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

问题描述

我有客户提供的以下 XML,我需要从中提取以下项目:

I have the following XML provided by a customer from which I need to extract items like:

  • <产品名称>

将每个 部分视为一个单独的订单.

Treating each <CustomerProducts> section as a seperate order.

我可以毫无问题地访问 并提取 并迭代每个部分中的值.

I can get to <CustomerProducts> and extract the <CustomerProductName> without a problem and iterate over the value in each section.

但是我看不到它下面的值是如何得到的.如果有人能给我一些关于如何实现这一点的指示,我将不胜感激,因为我已经尝试了一天多了.

But I can't see how I get to the values below it. If someone could someone give me some pointers as to how to achieve thIs would be gratefull as I have been trying for over a day now.

基本代码在XML下面

<?xml version="1.0"?>
<Products_Root>
    <Products_IPN VendorID="11344" >
        <CustomerProducts CustomerProductName="Test" ProductCount="7">
            <Products ProductType="BOOK" ProductName="Donald" >
                <ProductComponents ProductAssetName="Donald.pdf" />
                <ProductSpecs SpecClass="MEASUREMENT" SpecType="MARGINS" SpecValue="PER FILE"/>
                <ProductSpecs SpecClass="OPERATION" SpecType="BIND" SpecValue="SADDLE"/>
            </Products>
            <Products ProductType="BOOK" ProductName="Duck">
                <ProductComponents ProductAssetName="Duck.pdf"/>
                <ProductSpecs SpecClass="MEASUREMENT" SpecType="MARGINS" SpecValue="PER FILE"/>
                </Products>
        </CustomerProducts>
        <CustomerProducts CustomerProductName="Test2" ProductCount="2">
            <Products ProductType="BOOK" ProductName="Micky">
                <ProductComponents ProductAssetName="Mouse.pdf" />
                <ProductComponents ProductAssetName="Mouse.pdf" />
                <ProductSpecs SpecClass="MEASUREMENT" SpecType="MARGINS" SpecValue="PER FILE"/>
            </Products>
            <CustomerProductSpecs SpecClass="OPERATION" SpecType="KITTING" SpecValue="SHRINKWRAP KIT"/>
        </CustomerProducts>
        <CustomerProducts CustomerProductName="Test3" ProductCount="6">
            <Products ProductType="BOOK" ProductName="Minnie">
                <ProductComponents ProductAssetName="Mouse" />
                <ProductSpecs SpecClass="MEASUREMENT" SpecType="MARGINS" SpecValue="PER FILE"/>
            </Products>
        </CustomerProducts>
    </Products_IPN>
</Products_Root>

这里是我的 VBScript 代码

And here my VBScript code so far

Dim xmlDoc, objNodeList, plot

Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load("E:\dropbox\Dropbox\Hobbs\Xerox Example Files\test.xml")

Set objNodeList = xmlDoc.getElementsByTagName("CustomerProducts")

plot="No Value"
If objNodeList.length > 0 then
    For each x in objNodeList
        JobName=x.getattribute("CustomerProductName")
        msgbox JobName
    Next
Else
    msgbox chr(34) & "CustomerProducts" & chr(34) & " field not found."
End If

推荐答案

您已经将选择语言设置为 XPath,也许您也应该使用它.:)

You already set the selection language to XPath, maybe you should use it, too. :)

Option Explicit

Dim xmlDoc, CustomerProducts, Products
Dim plot, CustomerProductName, ProductName

Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load "products.xml"

plot = "No Value"

For Each CustomerProducts In xmlDoc.SelectNodes("//CustomerProducts")
  CustomerProductName = CustomerProducts.getAttribute("CustomerProductName")

  For Each Products In CustomerProducts.SelectNodes("./Products")
    ProductName = Products.getAttribute("ProductName")

    MsgBox CustomerProductName & " - " & ProductName
  Next
Next

这篇关于VBScript 遍历 XML 子节点并检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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