Navigating XML nodes in VBScript, for a Dummy

I am trying to write a script that will manipulate some data for me in an xml file. I am pretty new to VBScript but have a VB.NET and VBA background, so I feel like I kind of know what I am doing.

I thought there is probably a better way to navigate the file rather than a lot of calling InStr() or similar for each line to see if what I am looking for is there. My initial idea was to use a few methods I have seen in VB.NET from System.XML, since I had seen node navigating functions and members in that.

After investigating this, I cannot find any way to import a namespace (System.XML, or otherwise) into VBScript without it running on a webpage. I decided to look for other options instead of spending more time searching for this.

Turns out there are other ways to do what I want, using methods and objects that specifically deal with navigating nodes of an XML file. I learned that some common examples of these "systems" (for lack of a better term, because I am sure that is improper) seem to be DOM and XPath.

I started by investigating XPath (since I had seen XPath deemed superior to DOM in a few places, such as: Traversing all nodes in an XML file with VBScript). I could not find anything to discribe the basics of XPath in vbscript. There is lots on the syntax for paths and such, but I could find nothing that describes the very basics of how to actually call that syntax in VBScript to make use of it. So I moved on to the next option.

I then found many slightly different articles/questions/etc about DOM. So I gave it a try. Not a single one of them worked, all gave me errors. Mostly, it just seemed that a DOM object is never loaded correctly. Here are just a few of the methods I have tried for this:

From MSDN: a beginner's guide to XML DOM:

Set objParser = CreateObject( "Microsoft.XMLDOM" )
Dim xDoc As MSXML.DOMDocument
Set xDoc = New MSXML.DOMDocument

If xDoc.Load("C:\My Documents\cds.xml") Then
   msgbox("Success!")
Else
   msgbox("Failure!")
End If

This returned failure every time.

Based on another method:

dim xmlDom
set xmlDom = createobject("MSXML2.DOMDocument")
xmlDom.async = false
xmlDom.load ("C:\MyFileLocation\MyFile.xml")

and then I tried a few things to detect if it worked like message boxes for each node name in xmlDom.documentElement.

I have tried so many other things I can't even remember most of them.

I simply don't know what more I can try or why this isn't working for me. I am simply at a loss for what more I can try differently while still having syntax that COULD work.

So my question is: How can I navigate an XML file using VBScript without the script being imbedded in a webpage or otherwise? I need to know the extreme basics.

I know my question likely seems stupid and ignorant since this should be easily available information, but I really cannot for the life of me find the basics I need to understand how to navigate nodes in ANY WAY using JUST VBScript (not in an html or asp file or something like that).


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.