About Me

Gaurav Seth is a a Senior Automation Test Analyst with experience in QTP, Selenium and Ranorex tools and currently working on Automating in C# for a FX trading Client

XML With QTP

Following Snippet of code will Print the contents of the XML File

Set xmlObj = XMLUtil.CreateXML()
xmlObj.LoadFile("C:\artifacts.xml")
Print xmlObj.ToString()
'Using a XML file stored on the hard drive




' This will Load any Web Page into an XML File and Print to see the ContentsSet xmlObj = XMLUtil.CreateXML()
xmlObj.LoadFile("http://www.w3schools.com/xml/cd_catalog.xml")
Print xmlObj.ToString()






'The Following Snippet of Code will create an XML File and add Chidren (Nodes) including values
Set doc = XMLUtil.CreateXML()

doc.CreateDocument "Papa"

Set root = doc.GetRootElement()
root.AddChildElementByName "Bambino","Valuable"

doc.SaveFile "C:\one..xml"


' The Following snippet of code will Load an XML File and Save it to another place giving it a different name
Set doc = XMLUtil.CreateXML()

doc.LoadFile "C:\artifacts.xml"

doc.SaveFile "C:\artifacts1.xml"

' Following snippet of code will display the name of the particular child and display the occurences of this
Set doc = XMLUtil.CreateXML()

doc.LoadFile "C:\artifacts.xml"

Set root = doc.GetRootElement()

Set children = root.ChildElements()

if children.Count() <> 0 then

Set firstChild = children.Item(2)

firstChildName = firstChild.ElementName()

Set sameNameChildren = children.AllItemsByName(firstChildName)

msg = "My first child name is " + firstChildName + " and I have " + FormatNumber(sameNameChildren.Count(),0) + " of them."

End If

msgbox msg


' Using the below snippet of code in the Above Mentioned XML will tell you how many children (nodes) does XML Have.
msgbox (children.count)


Parsing the XML

Once we have the XML data loaded with the XML object pointing to it, our job is very simple. We need to navigate through the structure looking for the data we want. In this example we wish to retrieve all the data related a particular CD in the CD Catalog. So what we do is that we utilize the ChildElementsByPath(Path) method do retrieve the data from each element of the XML. Here, "Path" is something common to the kind of path we specify in our PC for locating a particular file. Here, each XML tag can be considered as a folder with the XML Element value as a file. So the topmost folder becomes CATALOG, then comes CD, then (TITLE, ARTIST, COUNTRY, COMPANY, PRICE,YEAR) become the individual folders with CD. So in case you wish to find the Title of a particular CD, you use ChildElementsByPath("/CATALOG/CD/TITLE"). This will actually return a collection of titles of all the Cds and you can iterate through each to get your desired CD title. A similar approach may be employed for getting the other data like, artist, company, price and year. This is just a simple example, there are a number of other XMl methods in QTP that can be used as well. Have a look at the code below -


Set xmlObj = XMLUtil.CreateXML()
xmlObj.LoadFile("http://www.w3schools.com/xml/cd_catalog.xml")
Print xmlObj.ToString()
Print " ========================================================================= "


Set myCDTitle= xmlObj.ChildElementsByPath("/CATALOG/CD/TITLE")
'Get the titles
Set myCDArtist = xmlObj.ChildElementsByPath("/CATALOG/CD/ARTIST")
'Get the Artists
Set myCDCountry = xmlObj.ChildElementsByPath("/CATALOG/CD/COUNTRY")
'Get the Country
Set myCDPrice = xmlObj.ChildElementsByPath("/CATALOG/CD/PRICE")
'Get the Prices
Set myCDYear = xmlObj.ChildElementsByPath("/CATALOG/CD/YEAR")
'Get the Year

'Now iterate through the collection to get the values
For i = 1 to myCDTitle.Count
Print myCDTitle.Item(i).Value()
Print myCDArtist.Item(i).Value()
Print myCDCountry.Item(i).Value()
Print myCDPrice.Item(i).Value()
Print myCDYear.Item(i).Value()
Print " "
Next

2 comments: