2012-08-22 63 views
4

我有一个客户提供以下XML从中我需要提取类的物品:的VBScript通过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 

回答

6

你已经设置选择语言的XPath,也许你应该使用它,太。 :)

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

Set xmlDoc = CreateObject("Msxml2.DOMDocument") 
xmlDoc.setProperty "SelectionLanguage", "XPath" 
xmlDoc.load("E:\dropbox\Dropbox\Hobbs\Xerox Example Files\test.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 
+0

托默勒格,感谢您的 – Greybeard

+0

发送后,必须记住,)
要恼火到下部结构等,我使用'对于Each'过程或可以我以另一种方式对它们进行了参考,例如
'ASSET =“./Products/ProductSpecs”
Greybeard

+0

我的答案显示了使用XPath('SelectNodes()')的方式。如果你不想循环遍历节点,那么你不需要使用'For Each'。查看[文档](http://msdn.microsoft.com/en-us/library/windows/desktop/ms761386(v = vs.85).aspx)以查明其他内容将对您有所帮助方法和属性可用。 – Tomalak