Looping through XML file using VBScript

vbscript

I have a XML file below ..I want to loop through this file and extract the node node value like for node com get the name value and then loop 2 times to get the file values.

<common>
  <com name="Test1.css">
    <file name="Tech.css">
    <file name="Comp.css"> 
  </com>
  <com name="Test2.css">
    <file name="HR.css">
    <file name="HR2.css"> 
  </com> 
</common>

I started using the below VBScript to start looping but I am getting an error Object required:Root
Can someone please have a look and tell me what I am doing wrong here

Option Explicit

Dim Root

Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.async = False 
objXMLDoc.load("C:\test\combineXML.xml")    
Set Root = objXMLDoc.documentElement 
WScript.Echo Root
Set NodeList = Root.getElementsByTagName("common")     
For Each Elem In NodeList 
WScript.Echo Elem.text

Next

Best Answer

Your error Object required:Root indicates that there was a parsing error during objXMLDoc.load() (because that's the only situation where documentElement would be set to Nothing).

Check objXMLDoc.parseError.

In fact you must always check if there were parse errors after loading an XML document.

On a more general note you should never use object references like documentElement without checking that they are valid. That's bad style.


Hint: The root node and the document element are two different things.

  • The root node is / (essentially, the root node is the document)
  • The document element in your case is <common>
  • This means: objXMLDoc.documentElement.getElementsByTagName("common") will be empty
Related Topic