VBScript Read XML File – How To???

So you have an XML file and you want to use it with VBSCript….
In this post we will review reading an XML file by knowing the TAG names, next time we will break down on opening an XML file without knowing the tags.

Let’s take a look at this xml file (if you don’t know what’s XML then go here linky).

I have multiple tags with values, if you know the XML tag

<?xml version='1.0' encoding='UTF-8'?>
<!-- This document was created with Syntext Serna Free. --><YeshaiUpgrade xmlns="Yeshai Upgrade Package">
      <Notes>Just Some Notes About The Package</Notes>

First thing we need to reference the XML object

	Set xmlDoc = CreateObject("Msxml2.DOMDocument") 

now we are going to make a reference to our XML file
You can set this how ever you want but I’m going based on the fact I don’t know the path to where the file will be (this is not mandatory)

		sPackageFile = sCurPath & "\UpgradePackage.xml"

Next step is referencing each and every tag you want to pull from the XML file

		FirstPackage = XMLRead (sPackageFile,"FirstPackage")
		UpgradeType = XMLRead (sPackageFile,"UpgradeType")
		NewVersion = XMLRead (sPackageFile,"NewVersion")
		OldVersion = XMLRead (sPackageFile,"OldVersion")
		UpgradeBaseFolder = XMLRead (sPackageFile,"UpgradeBaseFolder")
		Reboot = XMLRead (sPackageFile,"Reboot")
		ExecuteEndSciprt = XMLRead (sPackageFile,"ExecuteEndSciprt")
		ShutDownServices = XMLRead (sPackageFile,"ShutDownServices")
		ShutDownXMLFile = XMLRead (sPackageFile,"ShutDownXMLFile")
		ExecuteAfterReboot = XMLRead (sPackageFile,"ExecuteAfterReboot")
		ExecuteAfterRebootScript = XMLRead (sPackageFile,"ExecuteAfterRebootScript")
		Notes = XMLRead (sPackageFile,"Notes")

Now I love functions so just for y’all I’ve added this function

Function XMLRead(XMLFileName, XMLTag)
		Set ElemList = xmlDoc.getElementsByTagName(XMLTag)
		XMLRead = ElemList.item(0).Text

End Function

After you run the process each one of the values is read from the XML file…

Quite simple 🙂

This entry was posted in Scripting and tagged , , , , , . Bookmark the permalink.