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">
  <Upgrade>
    <Version>
      <FirstPackage>Yes</FirstPackage>
      <UpgradeType>YeshaiMainComp</UpgradeType>
      <NewVersion>Yeshai.57</NewVersion>
      <OldVersion>Yeshai.50</OldVersion>
      <UpgradeBaseFolder>C:\Yeshai</UpgradeBaseFolder>
      <Reboot>No</Reboot>
      <ExecuteEndSciprt>Upgrade.vbs</ExecuteEndSciprt>
      <ShutDownServices>Yes</ShutDownServices>
      <ShutDownXMLFile>SomeFile.XML</ShutDownXMLFile>
      <ExecuteAfterReboot>Yes</ExecuteAfterReboot>
      <ExecuteAfterRebootScript>Phase2.vbs</ExecuteAfterRebootScript>
      <Notes>Just Some Notes About The Package</Notes>
    </Version>
  </Upgrade>
</YeshaiUpgrade>

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)
		xmlDoc.load(XMLFileName) 
		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.