Navigating XML nodes in VBScript, for a Dummy

Here is a small example:

Suppose you have a file called C:\Temp\Test.xml with this contents:

<?xml version="1.0"?>
<root>
   <property name="alpha" value="1"/>
   <property name="beta" value="2"/>
   <property name="gamma" value="3"/>
</root>

Then you can use this VBScript:

Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:\Temp\Test.xml"

' Iterate over all elements contained in the <root> element:

Set objRoot = objDoc.documentElement
s = ""
t = ""
For Each child in objRoot.childNodes
   s = s & child.getAttribute("name") & " "
   t = t & child.getAttribute("value") & " "
Next
MsgBox s    ' Displays "alpha beta gamma "
MsgBox t    ' Displays "1 2 3 "

' Find a particular element using XPath:

Set objNode = objDoc.selectSingleNode("/root/property[@name="beta"]")
MsgBox objNode.getAttribute("value")     ' Displays 2

I hope this helps getting you started with VBScript and XML.

Leave a Comment