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.
  • Dan

    thanks for the article…I’m trying to create a vbscript to read values out of an xml file and your article is a good start for me….but I still have some questions…probably would be best for me to ask for a hypothetical example relative to your existing example…..
    assume that your xml file had 2 upgrades in it (see below) and you wanted the script to report the “NewVersion” for all upgrades that don’t require a reboot (Reboot = No). Can you show me an example of how you would achieve this? Thanks in advance….

    Yes
    YeshaiMainComp
    Yeshai.57
    Yeshai.50
    C:Yeshai
    No
    Upgrade.vbs
    Yes
    SomeFile.XML
    Yes
    Phase2.vbs
    Just Some Notes About The Package

    No
    YeshaiMainComp
    Yeshai.60
    Yeshai.57
    C:Yeshai
    No
    Upgrade.vbs
    Yes
    SomeFile.XML
    Yes
    Phase2.vbs
    Just Some Notes About The Package

  • Brandon

    Hi,

    I used your code like below. I was trying to extract the UPC value (last line in the XML extract below) and I get this error: “Object required: ‘ElemList.item(…)’ “. there are multiple ‘UPC’ fields in the XML. Can you see what I am doing wrong here? I am new to this and not a programmer by trade. I was trying to extract data from this xml and generate another XML file using the data. Any help you can give me would be appreciate. Thanks in advance.

    ———————————————————————————–
    sub test_()

    Dim upc

    sPackageFile = “C:test66056_PICK_130405144508.xml”

    upc = XMLRead (sPackageFile,”UPC”)

    msgbox “display: ” & upc

    end sub
    ———————————————————————————–
    Function XMLRead(XMLFileName, XMLTag)

    dim ElemList, xmlDoc

    Set xmlDoc = CreateObject(“Msxml2.DOMDocument”)

    xmlDoc.load(XMLFileName)

    Set ElemList = xmlDoc.getElementsByTagName(XMLTag)

    XMLRead = ElemList.item(0).Text

    End Function

    ———————XML extract—————————————————————–

    SOH

    999999999999

    British Bulldog

    71 Marylebone High St

    Marylebone

    W1U 5JN

    GB

    British Bulldog

    71 Marylebone High St

    London

    W1U 5JN

    GB

    209700

    2013-04-05T21:43:10Z

    GBP

    90.00

    0.00

    0

    72

    72

    0.00

    DPD

    ST

    4445555

    alfa@beta.com

    0

    12345

    Europe

    60188

    UK Ecomm Store

    ud4151900

    h3170493

    DPD STANDARD

    VISA

    VAT# 117 2074 43

    1

    0

    SOL

    660560116600209700

    000003343049

  • Rahul Jawale

    Thanks :)

  • Praveen

    Hi All, i have XML which is having some tag name like and im not able to read this tag. Script is throwing an error at ‘:’, Any help would be great.

  • saurabh

    Hi could you pls teach me step by step, how read data from xml?