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

Automation Guild Online Conference Top ad

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 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 comments
Daniel - June 29, 2015

Thank you Joe,

it was very useful.

Regards from Madrid

Reply
vijay prabakaran - October 30, 2015

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

Reply
Click here to add a comment

Leave a comment: