Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip first node and iterate from second node of a xml in VBS

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>
like image 235
Jyotish Singh Avatar asked Nov 09 '22 20:11

Jyotish Singh


1 Answers

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
like image 166
Ekkehard.Horner Avatar answered Nov 15 '22 06:11

Ekkehard.Horner