For example, we will use this XML file:
This XML file describes a bookstore and books located there.
Note: You can download this XML file here.
I will use Microsoft's XML Parser also known as Microsoft.XMLDOM. This XML parser allows running XPath queries.
The loading of XML file in QTP is simple enough:
Const XMLDataFile = "C:\TestData.xml"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
xmlDoc.Load(XMLDataFile)
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
xmlDoc.Load(XMLDataFile)
Several comments on this code:
- Microsoft.XMLDOM is a name of COM object of Microsoft's XML parser
- Async is a property of Microsoft.XMLDOM.
The property specifies whether asynchronous download of the document is permitted.
Processing of asynchronous operations is more complex, that's why I've disabled it (xmlDoc.Async = False). For datailed info see here. - Load method loads an XML document from the specified file.
Also, you can use LoadXML method, which loads an XML document using the supplied string.
After that we can use SelectSingleNode or SelectNodes of Microsoft's XML parser to execute XPath query.
You can use this approach in QTP to get data from XML file.
Check the above bookstore XML file again and let's see:
- How to get number of books in a bookstore?
The QTP script is:Set nodes = xmlDoc.SelectNodes("/bookstore/book")And its result is:
MsgBox "Total books: " & nodes.Length
As you can see, "/bookstore/book" XPath expression selects all "book" nodes in a "bookstore" nodes. As a result, we get the list of all books.
I hope, that's clear. Let's complicate the task a bit. - How to get titles of all books in a bookstore?
QTP script gets list of books from XML file and iterates them through:' get all titlesThe result is:
Set nodes = xmlDoc.SelectNodes("/bookstore/book/title/text()")
' get their values
For i = 0 To (nodes.Length - 1)
Title = nodes(i).NodeValue
MsgBox "Title #" & (i + 1) & ": " & Title
Next - How to get title of the first book?
QTP script is:Set node = xmlDoc.SelectSingleNode("/bookstore/book[0]/title/text()")Please see how QTP parses XML file and gets the required value:
MsgBox "Title of 1st book: " & node.NodeValue
Note: Pay attention that nodes are zero-indexed, i.e. first book is book[0], second book is book[1], and so on. - How to get titles of all John Smith's books?
QTP script is:' get list of John Smith's booksNote: We use square brackets to apply a filter. So, [../author = 'John Smith'] means 'to get only those books whose author is John Smith'.
Set nodes = xmlDoc.SelectNodes("/bookstore/book/title[../author = 'John Smith']/text()")
' get their titles
For i = 0 To (nodes.Length - 1)
Title = nodes(i).NodeValue
MsgBox "Title #" & (i + 1) & ": " & Title
Next
Since Mr. Smith wrote two books, we get the following result: - How to get titles of all books published after 2003?
QTP script is:' get list of books published after 2003Note: And again, we use square brackets to apply a filter - [@published > 2003].
Set nodes = xmlDoc.SelectNodes("/bookstore/book/title[@published > 2003]/text()")
' get their titles
For i = 0 To (nodes.Length - 1)
Title = nodes(i).NodeValue
MsgBox "Title #" & (i + 1) & ": " & Title
Next
At sign (@) is used to address an attribute of a node. For example:
Here, title node contains one attribute - published.
So, [@published > 2003] means 'to get only those books which were published after 2003'.
The result of above QTP script is:
You can check initial XML file and make sure that QTP parses it correctly.
Summary:
- Use Microsoft.XMLDOM to read and parse XML file from QuickTest Professional (QTP)
- XPath allows creating different queries to extract required data
- The present visual tutorial explains how to process XML file from QTP
Related articles:
- QTP RegExp VIDEO - How to click dynamic link?
- How to update XML file from QTP
- QTP Descriptive Programming - How to get number of objects
- QTP - How to get number of pages in PDF file?
- All QTP visual tutorials
Do you like this QTP tutorial? Would you like to receive them in the future?
If yes, please subscribe to this blog RSS feed or by Email. (How to subscribe? VIDEO guide)
--
Dmitry Motevich
9 comments:
Hi, this is a great tutorial. Have a question though. How can we get the published date [inline tag value], like the book names and the title?
Thanks.
Hi,
Its a great explanation with simple examples.apprecaite ur work..
hi,
I am so much grateful and thankful to you, whoever concerned, because currently i am practising on XML in qtp and its exactly what i wanted. Many thanks. But i need some more still like how can i traverse back from child node to parent node and retrive some other attribute value within a loop. i shall be very much happy if anyone can help me in this and send me directly to charles.vimala@gmail.com
thanks,
charles.
Great work work buddy.It really works
@ Charles
Please see this thread:
http://www.advancedqtp.com/forums/index.php/topic,3574.0.html
The notes were really useful and has clear crisp explanation.
I got all the tags from the xml to datatable to enable further verification.
Set smldoc = createobject("microsoft.xmldom")
smldoc.async = false
smldoc.load "C:\Documents and Settings\suganya\Desktop\qtp\one.xml"
Set nodes = smldoc.selectnodes("/catalog/book")
leng = nodes.length
Set nodeauth = smldoc.selectnodes("catalog/book/author/text()")
Set nodetitle = smldoc.selectnodes("catalog/book/title/text()")
Set nodegenre = smldoc.selectnodes("catalog/book/genre/text()")
Set nodeprice = smldoc.selectnodes("catalog/book/price/text()")
Set nodepubdate = smldoc.selectnodes("catalog/book/publish_date/text()")
Set nodedescription = smldoc.selectnodes("catalog/book/description/text()")
'datatable.
datatable.AddSheet("Results").addparameter "Author",""
datatable.AddSheet("Results").addparameter "Title",""
datatable.AddSheet("Results").addparameter "Genre",""
datatable.AddSheet("Results").addparameter "Price",""
datatable.AddSheet("Results").addparameter "Publishdate",""
datatable.AddSheet("Results").addparameter "Description",""
For i = 0 to (leng-1)
auth = nodeauth(i).nodevalue
title = nodetitle(i).nodevalue
genre = nodegenre(i).nodevalue
price = nodeprice(i).nodevalue
pubdate = nodepubdate(i).nodevalue
description1 = nodedescription(i).nodevalue
datatable.SetCurrentRow(i)
datatable("Author","Results") = auth
datatable("Title","Results") = title
datatable("Genre","Results") = genre
datatable("Price","Results") = price
datatable("Publishdate","Results") = pubdate
datatable("Description","Results") = description1
Next
datatable.Export "C:\Documents and Settings\suganya\Desktop\Book1.xls"
Charles,
I think the answer i have given to your question may be of use to you.
Please try, All the very best
Hi,
I found the above response very helpful .
But in my application i have to read the xml resonse that is dynamically generated while running the plan. Could you please let me know how to do it.
The scenario is like this:
I provide person datails like last name , DOB, SSN to a web service.It brings back all the policies associated to that person in one string.
response:
8009Pol............
i - policy number
I am not able to pubish the xml response for some reason ( it is not allwoing me to do )
now I have to provide 10 members using the datasheet in qtp and then find out the number of policies associated to each person. for this i have to read the xml page i get while running the plan and i am not able to do it.
Could you please help me in this regard..
thanks
vg
this is awesome tutorial, even with the snapshot which explains the result (ie msgbox result)
Post a Comment