In this article, I will show you how to read and update XML File in UFT using VBA.
In order to perform operations like reading, writing and updating data in XML File in UFT, we will use the COM object of Microsoft XML Parser. We can create the object of Microsoft XML Parser using the following statement.
Set oXML= CreateObject(“Microsoft.XMLDOM”)
To know more about COM objects please refer to Component Object Model in UFT.
The following methods will be used to parse the XML File.
- SelectSingleNode – Selects the first XML Node that matches the provided XPath pattern.
- SelectNodes – Selects a list of nodes matches the provided XPath pattern.
Read and Update XML File in UFT
We will use the following sample XML file to parse it.To locate a specific node in XML we will use XPath.
To know more about XPath please refer to XPath with Example.The article is available under Selenium Category however, it is a generic article and it will work with any language.
Read Value From a Single XMLNode Matching the XPath Pattern in UFT
1 2 3 4 5 |
Set oXML = CreateObject("Microsoft.XMLDOM") XMLFilePath = "C:\Book.xml" oXML.Load (XMLFilePath) Set BooksNode = oXML.SelectSingleNode("*//book[1]/name/text()") 'index value starts with 0, Selecting 2nd Node MsgBox BooksNode.NodeValue ' MsgBox will display PHP Tutorial |
Output
PHP Tutorial
Read Values from List of XMLNodes Matching the Xpath Pattern in UFT
1 2 3 4 5 6 7 8 |
Set oXML = CreateObject("Microsoft.XMLDOM") XMLFilePath = "C:\Book.xml" oXML.Load (XMLFilePath) Set BooksNameNode = oXML.SelectNodes("*//book/name/text()") 'Selecting all name nodes under book node For i = 0 To (BooksNameNode.Length - 1) BookName = BooksNameNode(i).NodeValue MsgBox BookName Next |
Output
Java Tutorial
PHP Tutorial
Visual Bsic Tutorial
CSharp Tutorial
Read XMLNodes Attribute Value in UFT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Set oXML = CreateObject("Microsoft.XMLDOM") XMLFilePath = "C:\Book.xml" oXML.Load (XMLFilePath) Set BooksNode = oXML.SelectNodes("/BookStore/book") 'Selecting all book nodes For i = 0 To (BooksNode.Length - 1) AttributValue = BooksNode(i).getAttribute("lang") 'Reading values for lang attribute MsgBox AttributValue Next |
Output
Java
PHP
VB
CSharp
Read XMLNodes Tag Name in UFT
The following example will read values of lang attribute for all book nodes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Set oXML = CreateObject("Microsoft.XMLDOM") XMLFilePath = "C:\Book.xml" oXML.Load (XMLFilePath) Set BooksNode = oXML.SelectNodes("/BookStore/book") For i = 0 To (BooksNode.Length - 1) For j = 0 To BooksNode(i).ChildNodes.Length - 1 MsgBox "Node Name is: <" & BooksNode(i).ChildNodes(j).tagname & "> and Node Value is: <" & BooksNode(i).ChildNodes(j).Text & ">" Next Next |
Output
Node Name is: <name> and Node Value is: <Java Tutorial>
Node Name is: <price> and Node Value is: <$5.00>
Node Name is: <name> and Node Value is: <PHP Tutorial>
Node Name is: <price> and Node Value is: <$4.75>
Node Name is: <name> and Node Value is: <Visual Bsic Tutorial Tutorial>
Node Name is: <price> and Node Value is: <$3.50>
Node Name is: <name> and Node Value is: <Charp Tutorial>
Node Name is: <price> and Node Value is: <$7.50>
Update XMLNode Attribute Value in UFT
In the following example,We will update the attribute value of lang attribute from Java to CoreJava for the first book node.
1 2 3 4 5 6 7 8 9 10 11 |
Set oXML = CreateObject("Microsoft.XMLDOM") XMLFilePath = "C:\Book.xml" oXML.Load (XMLFilePath) Set oAttribute = oXML.SelectSingleNode("/BookStore/book[0]/@lang") 'Selecting first book node oAttribute.Text = "CoreJava" ' Updating Attribute value to CoreJava oXML.Save (XMLFilePath) |
Conclusion
In this post,we have learned how to read and update XML nodes and attributes values.Hope you will find this artcile helpful.Please don’t forget to share it and like it.
Recommended Posts
- Most Useful VBScript Functions That You Must Know
- How to Use Visual Relation Identifier in UFT with Example
- Find a Matching Pattern in String Using Regular Expression in UFT
- How to Use Shell Scripting in UFT to SendKeys
- Transactions in UFT with Example
- How to Use XPath in UFT One To Identify Web-Based Objects
- Read, Write and Update Excel File In UFT
- VBScript Loops in UFT
- 20 VBA Date Functions in UFT That You Must Know