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")
MsgBox "Total books: " & nodes.Length
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:' 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: - 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:
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 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: - 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: - You can check initial XML file and make sure that QTP parses it correctly.
Source http://motevich.blogspot.com
Reviews:
Post a Comment