How to use QTP and Microsoft.XMLDOM to verify an element value in XML

QTP/UFT Published on:
Code Hacker

I recently had to test a .NET grid that did not have a way for me to easily see its row/cell data. After doing a little digging, I was able to find a method that returned to me the full text of a specified row. The only issue was that it was in an XML format. Luckily, though, I was able to get the cell data I needed by using the COM interface Microsoft.XMLDOM to create a QTP parser function.

Get text from specified node QTP example:

Here is an abridged version of the XML string that the grid’s.NET method was returning to me:

currentRow = currentRow = “<schAppt><ApptDate></ApptDate><ApptDay>THU</ApptDay><ApptNumber>37015</ApptNumber><ApptSetAnchorFlag></ApptSetAnchorFlag><Copay></Copay><Department>TA PRIMARY CARE</Department><ProviderName>TAPRIM2</ProviderName></schAppt>”

‘ First create a instance of the Microsoft XMLDOM object.

set xmlDoc = CreateObject(“Microsoft.XMLDOM”)

‘Next point to the XML string.

xmlDoc.Async=”false”

‘If this was a xml file rather then a xml string you would use xmlDoc.Load(“C:\myXML.xml”) instead
xmlDoc.LoadXml(currentRow)

‘ Find the specified element
strSearchNode = “ApptNumber”
‘getElementsByTagName returns a NodeList of all a elements with the specified element name

Set NodeList = xMLDoc.getElementsByTagName(strSearchNode)

For Each Node In NodeList
strCurrentNodeValue = node.text
strNodeFoundFlag = “True”
Next

If strNodeFoundFlag = “True” Then
appt = strCurrentNodeValue
Else
appt = “No node found matching the name you specified”
End If

‘This will Return the value of 37015
msgbox appt

To see this code in action check out my video post: How to test a web service with QTP

3 responses to “How to use QTP and Microsoft.XMLDOM to verify an element value in XML”

  1. could you please tell me how to retrive the content from this XML file??

    C:/program files/nmap

    C:/QTP/Nmap/Output

    1

    2

    3

    4

    1

    2

    3

    4

    0

    OXE_call_server

    TCP

    1

    172.19.20.167

    1

    OXE call server

    UDP

    1

    172.19.20.167

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Code Hacker