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:
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:
And its result is:
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:
The result is:
- How to get title of the first book?
QTP script is:
Please see how QTP parses XML file and gets the required value:
Note: Pay attention that nodes are zero-indexed, i.e. first book is book, second book is book, and so on.
- How to get titles of all John Smith's books?
QTP script is:
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:
- How to get titles of all books published after 2003?
QTP script is:
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:
You can check initial XML file and make sure that QTP parses it correctly.
- 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
- 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)