2015-01-21 70 views
0

我有自己的头衔和等级DVD的以下XML:XML查询到的字符串

<Collection> 
    <DVD> 
    <Title>Wild Down Under</Title 
    <Rating>3 Stars</Rating> 
    </DVD> 
    <DVD> 
    <Title>Up</Title 
    <Rating>5 Stars</Rating> 
    </DVD> 
</Collection> 

而且我有下面的VBScript来遍历和回声出DVD标题,这工作好

Set xmlDoc = CreateObject("Microsoft.XMLDOM") 
xmlDoc.Async = "False" 
xmlDoc.Load("dvdcoll.xml") 
strQuery = "/Collection/DVD/Title" 
Set colNodes = xmlDoc.selectNodes(strQuery) 
For Each objNode in colNodes 
    dvdTitle = objNode.text 
    WScript.Echo dvdTitle 
Next 

但我想要做的是以某种方式将“评分”节点添加到字符串中。任何想法我可以做到这一点?评分和标题是分开的字符串,这一点很重要。

+0

类似于下一个问题:[用vbscript解析xml](http://stackoverflow.com/q/26144567/3439404) – JosefZ 2015-01-21 11:35:55

回答

1

(1)修正你的.xml

(2)使用一个共同的skeleton/structure/strategy为您的VBS代码处理XML(支票,诊断输出)

(3)遍历所有DVD节点和访问他们的标题和评级节点 - 在

Option Explicit 

Dim xmlObj : Set xmlObj = CreateObject("msxml2.domdocument") 
xmlObj.async = False 
xmlObj.Load "..\data\28065845.xml" 
If xmlObj.parseError.errorCode <> 0 Then 
    WScript.Echo "Error Reading File - " & xmlObj.parseError.reason 
Else 
    Dim sXPath : sXPath  = "/Collection/DVD" 
    Dim ndlDVD : Set ndlDVD = xmlObj.selectNodes(sXPath) 
    If 0 = ndlDVD.length Then 
     WScript.Echo "failed:", sXPath 
    Else 
     Dim ndDVD 
     For Each ndDVD in ndlDVD 
      Dim ndTitle : Set ndTitle = ndDVD.selectSingleNode("Title") 
      Dim ndRating : Set ndRating = ndDVD.selectSingleNode("Rating") 
      WScript.Echo "found:", ndTitle.text, ndRating.text 
     Next 
    End If 
End If 

输出:

cscript 28065845.vbs 
found: Wild Down Under 3 Stars 
found: Up 5 Stars