5

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

  • <CustomerProductName>
  • <ProductName>
  • <ProductAssetName>

将每个<CustomerProducts>部分视为单独的顺序。

我可以毫无问题地获取<CustomerProducts>并提取并迭代每个部分中的值。<CustomerProductName>

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

基本代码在 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 代码

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
4

1 回答 1

7

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

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
于 2012-08-22T14:36:12.810 回答