XML Parsing in QTP using XMLUtil - A Simple ExampleXML 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 titlesSet myCDArtist = xmlObj.ChildElementsByPath("/CATALOG/CD/ARTIST")'Get the ArtistsSet myCDCountry = xmlObj.ChildElementsByPath("/CATALOG/CD/COUNTRY")'Get the CountrySet myCDPrice = xmlObj.ChildElementsByPath("/CATALOG/CD/PRICE")'Get the PricesSet 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
ShareThis Labels: QTP 9.5, Quick Test Professional Knowledge Base
5 comments:
Maria Mcclain said...
You have a very good blog that the main thing a lot of interesting and beautiful! hope u go
for this website to increase visitor.
July 16, 2010 4:24 AM
Sudhananda Reddy Devarapalli said...
coooll... the same way we can use to test the web services as well with out using Web
Services add in ...
July 24, 2010 11:13 PM
Rishi said...
Hi, nice blog!!! Can u pls tell me how to check if a node element is present in an xml file?
August 2, 2010 6:43 AM
Dipendra said...
Thanks 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..
For 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
October 19, 2010 12:00 AM
Anunay Kumar said...
@ 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.
October 20, 2010 11:24 PM
Post a Comment
Links to this post
Create a Link
Newer Post Older Post Home
Subscribe to: Post Comments (Atom)
Appreciate My Efforts
Subscribe
Blog Archive
▼ 2010 (21)
o ▼ July (4)
Upload Files to a Box.net account using its API in...
Sending SMS using the HTTP based SMS API from QTP
Sending Emails using QTP via Gmail, Hotmail and ot...
XML Parsing in QTP using XMLUtil - A Simple Exampl...
o ► June (5)
o ► May (3)
o ► February (1)
o ► January (8)
Labels
Adobe Acrobat Automation (7)
Dot Net Factory (1)
Internet Explorer 8 (1)
Out of the Box (3)
QTP 9.5 (17)
QTP Comparison (1)
QTP Installation (1)
QTP Resources (1)
Quick Test Professional Knowledge Base (18)
QTP Resources
Buy from Flipkart
Blog Rankings
All rights reserved - learnqtp.info. Email us at [email protected]
http://www.learnqtp.info/2010/07/xml-parsing-in-qtp-using-xmlutil-simple.html
Translate
1 Online
Random
Share
Search...