2012-12-19 160 views

回答

2

如果您使用VBScript,我假設您有權訪問System.Xml。

此網頁上有一看: http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes.aspx

而在使用System.Xml.XPath周圍挖多一點在谷歌爲好,特別是外觀。那裏有很多例子。

這是我在用於測試的控制檯應用程序中編寫的測試方法。這是寫在C#但這個想法應該有所幫助:

private static void ExtractUserNodeFromUsersXml() 
    { 
     XmlDocument xmlDoc = new XmlDocument(); 

     string xml = @"<data xmlns=''><users><user id='33' culture='en-gb' /> 
<user id='38 culture='en-gb' /> 
<user id='285'culture='en-gb' /></users></data>"; 



     xmlDoc.LoadXml(xml); 

     string userid = "38"; 

     XPathNavigator nav = xmlDoc.CreateNavigator(); 

     XPathNodeIterator userNodes = nav.Select("data/users/user[@id='" + userGuid + "']"); 

     while (userNodes.MoveNext()) 
     { 
      if (userNodes.Current is IHasXmlNode) 
      { 
       XmlNode node = ((IHasXmlNode)userNodes.Current).GetNode(); 

       if (node != null) 
       { 
        string culture = node.Attributes.GetNamedItem("culture").Value; 

        Console.WriteLine(node.OuterXml); 
        Console.WriteLine("Culture is " + culture); 
       } 
      } 
     } 

     Console.WriteLine(); 
     Console.WriteLine("******"); 
     Console.WriteLine(); 

     Console.WriteLine(xmlDoc.OuterXml); 
    } 

這可能是你需要的東西有點大材小用,但如果你看看網上,並使用此代碼玩弄那麼它會幫助你。事實上,我現在要去爲你的XML改變這種方法。

將XPathNavigator更改爲XML。

XPathNodeIterator emailNodes = nav.Select("root/Lease/row"); 

請確保您的XML有效,並記住'xpath'區分大小寫。

2

VBScript的版本(最好與Docs方便使用):

' Assuming you have a string in gXML, I fake it here, please 
    ' note the closing of the row nodes! 
    Dim gXML : gXML = Join(Array(_ 
     "<root>" _ 
    , " <Lease>" _ 
    , " <row hello=""[email protected]""/>" _ 
    , " <row hello=""[email protected]""/>" _ 
    , " <row hello=""[email protected]""/>" _ 
    , " <row hello=""[email protected]""/>" _ 
    , " </Lease>" _ 
    , "</root>" _ 
), "") 
    Dim oXML : Set oXML = CreateObject("Msxml2.DOMDocument") 
    oXML.loadXml gXML 
    If 0 = oXML.ParseError Then 
    Dim ndlRow : Set ndlRow = oXML.selectNodes("/root/Lease/row") 
    If 0 < ndlRow.length Then 
     Dim nRow 
     For nRow = 0 To (ndlRow.length - 1) 
      WScript.Echo nRow, "send mail to", ndlRow(nRow).getAttribute("hello") 
     Next 
    Else 
     WScript.Echo "no rows found" 
    End If 
    Else 
    WScript.Echo oXML.parseError.reason 
    End If 

輸出:

0 send mail to [email protected] 
1 send mail to [email protected] 
2 send mail to [email protected] 
3 send mail to [email protected] 

取而代之的是計算環路的

For nRow = 0 To (ndlRow.length - 1) 
    WScript.Echo nRow, "send mail to", ndlRow(nRow).getAttribute("hello") 
Next 

你可以使用一個For Each Loop:

Dim ndRow 
For Each ndRow In ndlRow 
    WScript.Echo "send mail to", ndRow.getAttribute("hello") 
Next