How to Work with XML using VBScript : Read, Write and Modify

We can work with XML using VBScript in different ways. One way is by leveraging XMLDOM. XMLDOM is a programming interface to access and manipulate XML documents. The most amazing thing about XMLDOM is that it can be used with any language and operating system.

It allows us to change the structure of XML and read the XML. Additionally, we can write new nodes or elements in the XML.
DOM will be the entire XML document as a tree, a document level element is the root of the tree.
XMLDOM contains four main objects:

  1. XMLDOMDocument
  2. XMLDOMNode
  3. XMLDOMNodeList
  4. XMLDOMNamedNodeMap.

Each XMLDOM object has its own characteristics and methods. Let’s see how we can leverage them.

Now we will see how to validate an xml using VBScript and XMLDOM.

Validate XML using VBScript and XMLDOM

The XMLUtil object is used to read the XML file, the LoadFile method loads the xml file so that system can read the text from XML format in the specified file, then it returns a XMLData object.

Example code to read XML using VBScript

' The CreateXML method uses the XMLUtil object to create a XMLData object
Set doc = XMLUtil.CreateXML()
' Loading XML file used to check
doc.LoadFile "Test.XML"

'Check the XML document meets the specified XML schema
ans = doc.Validate ("D:Program Files\sample.xsd")
'If the inspection meeting Schema, prompt examination success, else make a list of 'failing reasons

If ans Then
 MsgBox "XML file matching the specified Schema!"
else
 errNo = doc.GetValidationErrorsNumber
For i = 1 to errNo
 errStr = doc.GetValidationError(i)
 MsgBox errStr
Next
End If

Modify XML using VBScript and XMLDOM

Now we will see how to modify an xml file. In this example, We created an object of XMLDOM and then loaded the the xml as before.

Once the xml is loaded, then we modify the nodes as per our requirement. As shown below, we traverse through the nodes by using index of the nodes. In the below example we have used print statement as per UFT/QTP. If you have to do it in pure VBSCript, you can use MsgBox instead of print statements.

Example code to modify xml using VBScript

' Create a XMLDOM object
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
' Load the XML document
xmlDoc.load "test.xml" 
' Check if there is an error in XML document
If xmlDoc.parseError.errorCode <> 0 Then
 Set myErr = xmlDoc.parseError
 MsgBox("XML Loads Failed. " & myErr.reason)
Else
 Set rootNode = xmlDoc.documentElement
' Change the value of an attribute of the specified node XML
 rootNode.childNodes(0).childNodes(0).childNodes(0).attributes(4).nodeValue = "E-Mail"
' Print the modified nodal values
 Print rootNode.childNodes(0).childNodes(0).childNodes(0).attributes(4).nodeValue
'Modify the node value 
 rootNode.childNodes(0).childNodes(0).childNodes(0).attributes(5).nodeValue =  "hello!"
'Print the modified nodal values
 Print rootNode.childNodes(0).childNodes(0).childNodes(0).attributes(5).nodeValue
' Save the XML data to another file
 xmlDoc.save "test_save.xml"
End If
Set xmlDoc = Nothing

Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts