I love to use XML Parser. It is very easy to do by Visual basic. You can get the equivalent visual test code here - Visual Test - MS XML Parser. I'm thinking to use it in Quick Test Professional (QTP). Below code snippets are simple and easy to use.
Case1: To load any XML content
'-----------------------------------------
' Method : XMLParserLoadXML
' Author : T.Palani Selvam
' Purpose : Creates XML Parsing object according to the given XML String.
' Parameters: sXML - String, contains data in XML Format.
' Returns : Returns a Object, which contains XML Parsing object.
' Caller : - Nil
' Calls : - Nil -
'------------------------------------------
Function XMLParserLoadXML(sXML As String) As Variant
Dim objXML As Variant
XMLParserLoadXML = Null
Set objXML = CreateObject("Microsoft.XMLDOM") 'Declare a Object
objXML.async = False
objXML.loadXML (sXML) 'Load xmlformat String
'OleSetProperty(objXML,"preserveWhiteSpace") = False
If (objXML.parseError <> 0) Then
LogWrite ("Parsing Error in the File :" + sXMLFile)
Exit Function
End If
'Top Node of given file
Set XMLParserLoadXML = objXML.documentElement
End Function
Case2: It parsed the given tag in given content and returns the node.
'-----------------------------------------
' Method : XMLParser
' Author : T.Palani Selvam
' Purpose : Creates XML Parsing object according to the given XML Format File and Element.
' Parameters: sXMLFile - String, contains a file name, which is in XML Format.
' sTag - String, contains element name from which Tag or element based object to be returned.
' Returns : Returns a Object, which contains XML Parsing object.
' Caller : - Nil
' Calls : - Nil -
'-----------------------------------------
Function XMLParser(sXMLFile As String, sTag As String) As Variant
Dim objXML As Variant, objNode As Variant, objTemp As Variant, objRoot As Variant
'Dim varTest As Variant
Dim iIndex As Integer, iTemp As Integer
'Dim objRoot As node
XMLParser = Null
sTag = Trim(sTag)
Set objXML = CreateObject("Microsoft.XMLDOM") 'Declare a Object
objXML.async = False
objXML.Load (sXMLFile) 'Load xmlformat file
If (objXML.parseError <> 0) Then
LogWrite ("Parsing Error in the File :" + sXMLFile)
MsgBox ("Parsing Error in the File :" + sXMLFile)
Exit Function
End If
'Top Node of given file
Set objRoot = objXML.documentElement
'Print "XML File is : " & sXMLFile
'Print objRoot.nodeName & " Name of the node.."
If ((sTag = "") Or (sTag = objRoot.nodeName)) Then
'XMLParser = CVar(objRoot)
Set XMLParser = objRoot
'Set varTest = objRoot
'XMLParser = objRoot
Exit Function
End If
If objRoot.hasChildNodes <> 0 Then
objTemp = objRoot.childNodes
iTemp = objTemp.length
For iIndex = 0 To iTemp - 1
objNode = objTemp.Item(iIndex)
If (UCase(Trim(objNode.nodeName)) = UCase(sTag)) Then
XMLParser = objNode
Exit Function
Else
Set objNode = Nothing
End If
Next iIndex
End If
objTemp = objXML.getElementsByTagName(sTag) 'Search whole file with sTag
iTemp = objTemp.length
If (iTemp > 0) Then
objNode = objTemp.Item(0)
XMLParser = objNode
End If
End Function
No comments:
Post a Comment