How to read XML file from QTP

How to read XML file from QTP

I'm going to show how to parse XML file and read its values from QTP (QuickTest Professional).
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)


Several comments on this code:
  1. Microsoft.XMLDOM is a name of COM object of Microsoft's XML parser
  2. 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.
  3. 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:

  1. How to get number of books in a bookstore?
    The QTP script is:
    Set nodes = xmlDoc.SelectNodes("/bookstore/book")
    MsgBox "Total books: " & nodes.Length

    And its result is:
    Number of books in a bookstoreAs 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.


  2. 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 titles
    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

    The result is:
    Titles of all books

  3. How to get title of the first book?
    QTP script is:
    Set node = xmlDoc.SelectSingleNode("/bookstore/book[0]/title/text()")
    MsgBox "Title of 1st book: " & node.NodeValue
    Please see how QTP parses XML file and gets the required value:
    Title of the first bookNote: Pay attention that nodes are zero-indexed, i.e. first book is book[0], second book is book[1], and so on.


  4. How to get titles of all John Smith's books?
    QTP script is:
    ' get list of John Smith's books
    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

    Note: We use square brackets to apply a filter. So, [../author = 'John Smith'] means 'to get only those books whose author is John Smith'.

    Since Mr. Smith wrote two books, we get the following result:
    Titles of John Smith's books

  5. How to get titles of all books published after 2003?
    QTP script is:
    ' get list of books published after 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

    Note: And again, we use square brackets to apply a filter - [@published > 2003].
    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:
    Titles of books published after 2003You 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:


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:

Anonymous said...

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.

Anonymous said...

Hi,
Its a great explanation with simple examples.apprecaite ur work..

charles said...

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.

kris said...

Great work work buddy.It really works

Anshoo said...

@ Charles

Please see this thread:

http://www.advancedqtp.com/forums/index.php/topic,3574.0.html

suganyarenga said...

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"

suganyarenga said...

Charles,
I think the answer i have given to your question may be of use to you.
Please try, All the very best

vizgud said...

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

JoS said...

this is awesome tutorial, even with the snapshot which explains the result (ie msgbox result)