2015-01-21 20 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