Sunday, January 23, 2011

XML Paresr using MSXML2.DOMDocument

Parsing XML file using QTP Example:

Public Function getXmlValueByTag(ByVal TagName, ByVal Attribs)
Dim xmlDoc, objNodeList, Attrib
Dim i, rndNum

Set xmlDoc =CreateObject("MSXML2.DOMDocument")
xmlDoc.load("D:\xmlfile.xml")

For h = 0 To UBound(Split(TagName, "\"))
Set objNodeList = xmlDoc.getElementsByTagName(Split(TagName, "\")(h))
If objNodeList.length > 0 Then
If ISEmpty(rndNum) And Split(TagName, "\")(h) <> "ASR" Then
rndNum = RandomNumber(1, objNodeList.length -1)
End If
Set Attrib = objNodeList.item(rndNum).attributes
If Attrib.length > 0 Then
For j = 0 To Attrib.length -1
For i = 0 To UBound(Split(Attribs, ","))
If Split(Attribs, ",")(i) = Attrib.item(j).name Then
print Attrib.item(j).name & "---" & Attrib.item(j).value
End If
Next
Next
End If
End If
Next
End Function

Print getXmlValueByTag ("COM", "P35C56")

>>>First parameter contain the tag name
>>> 2nd parameter contain the attribute name

Example: