Working with XML

We will see how to read the XML nodes,its sub nodes and the data.
Let’s start with a practical example. We will take the below XML as input.

How to Load the XML file?

We need to create the object for XML parsing.

1. We are creating the object for XML using “Msxml2.DOMDocument”.
2. We are loading it through load method.
3. Async has some specific significance here.

If async is true, then method returns the control to the caller before the download is complete.
If async is flase, then download must be completed before the caller gets the control back.


How to count the particular nodes?

Count Node
Specific Node count

Specific Node count


Get the first name text:

Get First Name Text
argXMLFilePath = “D:\Demo.xml”
Set oXMLFile = CreateObject(“Msxml2.DOMDocument”)
oXMLFile.Load(argXMLFilePath)
oXMLFile.async “False”
Set nodes=oXMLFile.SelectNodes(“/WorkerDetails/Worker/Personal/Name_Data
/First_Name”
)
‘ fetch the text
For i = 0 To (nodes.Length – 1)
FirstName = nodes.item(i).text
MsgBox “FirstName :” & FirstName
Next
Names


One of my friends asked for a help on one of his requirement and the requirement was like fetching all nodes, child of that nodes and their data into one excel file. Functions given below are generic. If you have same kind of requirement,then you just need to provide the required file paths and few arguments, Rest it will take care. Otherwise, you can have some idea from this to fulfil your specific requirement. I am sharing that code, so it will helpful for needed.I will tell you in the end,How to call these functions.

You can use the same XML and the functions to see how it works. You will require 3 functions as given below.

First Function:


Second Function:

Third Function:


You can call these functions as shown below.

It will write the data from second row in the excel sheet. The columns in excel are explained below
Column 1: Parent node
Column 2: Child Node
Column 3: no. of time the subchilds are repeated for its parent
Column 4: Subchild, if any
Column 6: Text data of nodes

You can provide column header as per your need. The excel will look like this as given below

ExcelData

 

Print Friendly, PDF & Email
Integrating with ALM
Object Identification mechanism
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *