如何使用vbscript迭代奇数编号的xml节点属性?

时间:2017-05-19 01:10:15

标签: vbscript

所以我试图只迭代xml文件中的奇数节点。名为的节点,其中x是1到32之间输入的值。

Option Explicit

Dim xmlDoc, i, colNodes, objNode
Set xmlDoc = _
  CreateObject("Microsoft.XMLDOM")

xmlDoc.Async = "False"
xmlDoc.Load("C:\Scripts\_setup.xml")


Set colNodes=xmlDoc.selectNodes("//input") 

For Each objNode in colNodes

   objNode.selectSingleNode("off_text").text = "me!"
   objNode.selectSingleNode("on_text").text = "you!"

   End If

Next

xmlDoc.Save "C:\Scripts\_setup.xml"

1 个答案:

答案 0 :(得分:0)

您可以将for循环修改为:

intCount = colNodes.Length
For i=0 to intCount-1 step 2
   colNodes.item(i).selectSingleNode("off_text").text = "me!"
   colNodes.item(i).selectSingleNode("on_text").text = "you!"
Next

因此,只选择节点0,2,4,6 ..... 30,它们具有属性值' id' = 1,3,5,7 ..... 31(奇数,如您所需)

另一种方法是:

Set colNodes = oxml.selectnodes("//input")
intCtr = colNodes.length
For i=1 To intCtr
    If i Mod 2 = 1 Then
        Set tempNode = oxml.selectsinglenode("//input[@id='"&i&"']")
        tempNode.selectsinglenode("off_text").text = "me!"
        tempNode.selectsinglenode("on_text").text = "you!"
    End If
Next