XML parsing in QTP is not a very difficult concept rather it is the most easiest of concepts in QTP. However, there are times when you have not worked on XML before and there is a task at hand and you do not wish to spend time reading QTP Help or going on the web spending a lot of time. I am showcasing a very simple XML structure and how to parse data out of it. There are a lot of examples on the web but none of them is a full fledged one. It rather showcases a particular functionality and never shows a complete solution from getting a XML loaded and finally extracting values from it.
Here, i assume that you have a basic understanding of XML schema and what you mean by extracting data from it. The basic concept in QTP is to create an object of the XML data that you have at hand either in the form of a explicit file stored at a location or the XML data in the form of string in a variable.
Loading XML data from a File/URL
You can load an XML file in QTP which then gives you access to the entire XML structure of the file. You can then use the XMLUtil functions on the XML data and get your desired task done. This is how we do it -
Set xmlObj = XMLUtil.CreateXML() xmlObj.LoadFile("http://www.w3schools.com/xml/cd_catalog.xml") Print xmlObj.ToString()OR
Set xmlObj = XMLUtil.CreateXML() xmlObj.LoadFile("C:\cd_catalog.xml") Print xmlObj.ToString() 'Using a XML file stored on the hard drive
Loading XML data from a variable
It is handy sometimes, when you are testing a web application and have a chunk of XML data as a result of a property of an object (like innerHTML, outerHTML, etc) and you need the dynamic data from that XML to verify something. Here, you load the XML from a variable and then apply the XMlUtil methods on it. This is how we do it -
Set xmlObj = XMLUtil.CreateXML() xmlData = "PUT YOUR XML DATA WITH ENTIRE STRUCTURE HERE" ' xmlData will hold the entire content of the XML file xmlObj.Load(xmlData) Print xmlObj.ToString()
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
This comment has been removed by a blog administrator.
ReplyDeletecoooll... the same way we can use to test the web services as well with out using Web Services add in ...
ReplyDeleteHi, nice blog!!! Can u pls tell me how to check if a node element is present in an xml file?
ReplyDeleteThanks for letting us know the concept, could you please help me to understand, How can we validate the XML tags values are coming correct in Web Application page..
ReplyDeleteFor Example: There are customer details like First Name, Last Name, Manager Name etc which appear in front End and the data of customer details are coming from Different Application through XML. I need to compare the data in XML tag with web application and pass/fail the test case accordingly
I need the same thing
DeleteCan you please contact me if you have the answer in a code?
shalom2012@gmail.com
@ Dipendra - I believe you can simple capture the object and check the "value" or "innertext" property of that object with the data that you have on the xml. Ex - First Name is a textbox object which will have the value or innertext property containing what is actually getting displayed on the post. just compare this value with the xml data.
ReplyDeletePlease tell me if i have the multiple tags in Web Services response with same name. then how we can verify those.
ReplyDeleteLike
1
2
3
4
Hi
ReplyDeletedo you know a way to add namespace to xml from qtp?
I'm writing a script that mocks transaction file which should be pulled and processed by BizTalk and I have no idea how to do that.
I built a file with XMLUtil...
Thanks
Ok I'll answer it myself since after two days of searching I found a veeeery simple solution...
DeleteYou just have to create a sample xml file and import it into XML Warehouse and leave only first line...
then
xmlload=XMLWarehouse("sampleXMLname")
Set XMLDoc=XMLUtil.CreateXML
XMLDoc.Load xmlload
and now you can add whatever you want
Hi,
ReplyDeleteI have to create an xml file which looks something like below:
when I use something like
Set oXML=XMLUtil.CreateXML
oXML.CreateDocument(rootElement)
Set oRoot=oXML.GetRootElement
......
.....
oRoot.AddChildElementByName "subchild",""
I am getting it as
but as per our requirements it has to be
Please help me with this issue.
Thanks,
Neha
This is the way it should be...very nice examples
ReplyDeleteI need information on getting attributes in the XML.
ReplyDeleteHi, I have this question and thanks in advance if some one heads up.
ReplyDeleteIn my test I am creating user and encrypted password is in the xml in a table. So I need to First read the xml with QTP and extract the password. Also the password is in between these two tags "Password>" and "</Password". Please let me know with code.
Thanks
Gopal
Simple yet descriptive...
ReplyDeleteThis was a huge help to me. Thanks.
ReplyDeletehow to pass dynamic value to const xmldatafile in qtp
ReplyDeletefor example
Const XMLDataFile ="C:\sitemap\" &s
where s has some value
Very well explained...
ReplyDelete