5

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>

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate your solution, Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.