While working on a small project for one of my clients, I was doing some code in VBScript to deal with a small XML input file.  The real XML and script are much bigger and more complicated, but I stripped out some of it to use as an example.

A lot of search results for “vbscript+xml” that I’ve run across seem to come up with all sorts of things but I didn’t see a whole lot that focused on a flexible keyset approach.  That’s where you can define an arbitrary list of base keys to enumerate and then dive in to each using a consistent key=value query of element attributes.

In this example, the keyset is defined by the “groupList” variable.  In this example, it corresponds to the top-level keys in the XML file (categories and types).  Beneath each are the elements, which use the key=value format with “name” = “value”.  The key names are arbitrary (e.g. “dkey”) and you can modify any of this this to suit your preferences.  Basically, any of the Const assignments can be modified as long as you modify the related XML data element to suit.

In any case, I hope this is useful to you in some way.

VBscript Example

Const xmlFile = "dave.xml"
Const groupList = "categories,types"
Const IDkey = "dkey"
Const IDval = "value"
Const IDref = "name"
Const IDroot = "config"
Dim scriptPath, dataFile, objXML, groupSet, nodes
Dim objNode, keyName, keyText

scriptPath = Replace(wscript.ScriptFullName, _
 "\" & wscript.ScriptName, "")
dataFile = scriptPath & "\" & xmlFile
On Error Resume Next 
Set objXML = CreateObject("Microsoft.XMLDOM") 
objXML.async = False 
objXML.load dataFile
For each groupSet in Split(groupList, ",")
  nodes = "/" & IDroot & "/" & groupSet & "/" & IDkey
  For Each objNode In objXML.SelectNodes(nodes) 
    keyName = Trim(objNode.GetAttribute(IDref))
    keyText = Trim(objNode.GetAttribute(IDval))
    wscript.echo groupSet & ": " & keyName & " = " & keyText
Set objXML = Nothing

XML Example

<?xml version="1.0" encoding="utf-8"?>
    <dkey name="A" value="alpha" />
    <dkey name="B" value="bravo" />
    <dkey name="C" value="charlie" />
    <dkey name="D" value="delta" />
    <dkey name="1" value="first item" />
    <dkey name="2" value="second item" />
    <dkey name="3" value="a third item" />
    <dkey name="4" value="another item" />


Yes, PowerShell is easier, cleaner, faster, better, cooler, neater, niftier.  Keep in mind that this is only because more functionality has been encapsulated within the PowerShell base module library than WSH.

Using a more “normal” format XML document, you can enumerate the key/value pairs in two lines of code…

<?xml version="1.0"?>
    <category id="A" value="alpha" />
    <category id="B" value="bravo" />
    <category id="C" value="charlie" />
    <dkey name="1" value="first" />
    <dkey name="2" value="second" />

and the code to read this?

[xml]$stuff = Get-Content .\dave2.XML


There are lots of ways to store and retrieve data.  Text files (.ini, .csv, .txt) have been around for decades, and will probably never go away.  There’s also proprietary formats, such as Microsoft Office, which require use of an installed COM interface.  But XML is not only popular, it offers a more structured and intuitive way to arrange and locate data.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s