How To Read Xml File From Qtp

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View How To Read Xml File From Qtp as PDF for free.

More details

  • Words: 647
  • Pages: 5
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:

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. 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:

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:

Note: 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:

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:

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 Documents