Anyone could suggest the best way to skip first occurrence of first node of a XML and start iteration from second node. In the example below, I want to skip first occurrence of node "word" and start Iterating form second occurrence of node "word". Thanks in advance.
<words>
<word>
<name>Vowel</name>
</word>
<word>
<value>a</value>
</word>
<word>
<value>Vowel</value>
</word>
</words>
The child nodes of a node are collected in its childNodes collection. To skip nodes you need to loop over the childNodes by number/index instead of the more frequent For Each approach. In code:
Option Explicit
Dim sXPath : sXPath = "/words"
Dim oXDoc : Set oXDoc = CreateObject("Msxml2.DOMDocument.6.0")
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load "35359922.xml"
If 0 = oXDoc.ParseError Then
Dim ndWords : Set ndWords = oXDoc.selectSingleNode(sXPath)
If ndWords Is Nothing Then
WScript.Echo "|", sXPath, "| not found"
Else
WScript.Echo "found " & ndWords.childNodes.length & " nodes."
Dim i
For i = 1 To ndWords.childNodes.length - 1
WScript.Echo i, ndWords.childNodes(i).text
Next
End If
Else
WScript.Echo oXDoc.ParseError.Reason
End If
output:
cscript 35359922.vbs
found 3 nodes.
1 a
2 Vowel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With