In my first test automation project, I thought to include XML Parser. At that time, XML concept was new and used in many applications. Visual test supports COM DLL calls. I used Microsoft DOM XML parser. XML is a best choice to have the test data.
XML Development Center
XML FAQ
Below I have given the code snippets to parse the XML files.
Code1: To Parse an XML file.
'-----------------------
' Method : XMLParserLoadXML
' Author : T.Palani Selvam
' Purpose : Creates XML Parsing object according to the given XML formatted String, Element .
' Parameters: sXML - String, contains data in XML Format.
' sTag - String, contains element name from which Tag or element based object to be returned.
' Returns : Returns a variant data, which contains XML Parsing object.
' Caller : - Nil
' Calls : - Nil -
'--------------------------------
Function XMLParserLoadXML(sXML As String, sTag As String) As Variant
Dim objXML As Variant, objNode As Variant, objRoot As Variant, objTemp As Variant
Dim iIndex As Integer, iTemp As Integer
XMLParserLoadXML = NULL
sTag = Trim (sTag)
objXML = OleCreateObject ("Microsoft.XMLDOM") 'Declare a Object
OleSetProperty (objXML,"async","False")
OleDispatch (objXML, "loadXML", sXML) 'Load xmlformat String
'OleSetProperty(objXML,"preserveWhiteSpace") = False
If (OleGetProperty(OleDispatch(objXML, "parseError"), "errorCode") <> 0) Then
LogWrite ("Parsing Error in the String :" + sXML, 1)
Exit Function
End If
'Top Node of given file
objRoot = OleDispatch (objXML,"documentElement")
IF ((sTag = "") OR (sTag = OleGetProperty(objRoot, "nodeName"))) Then
XMLParserLoadXML = objRoot
Exit Function
End IF
If OleDispatch (objRoot ,"hasChildNodes") <> 0 Then
objTemp = OleDispatch (objRoot, "ChildNodes")
iTemp = OleGetProperty(objTemp, "Length")
For iIndex = 0 To iTemp -1
objNode= OleDispatch(objTemp, "item",iIndex)
If (Ucase(trim(OleGetProperty(objNode,"nodeName"))) = UCase(sTag)) Then
XMLParserLoadXML = objNode
Exit Function
Else
OleReleaseObject (objNode)
End If
Next iIndex
End IF
objTemp = OleDispatch(objXML,"getElementsByTagName",sTag) 'For test.xml
iTemp=OleGetProperty(objTemp,"Length")
If (iTemp > 0 ) Then
objNode = OleDispatch (objTemp, "item", 0)
XMLParserLoadXML = objNode
End IF
End Function
Code2: It parses the given tag and returns the content.
'-------------------------------------
' Method : XMLParser
' Author : T.Palani Selvam
' Purpose : Creates XML Parsing object according to the given XML Format File and Element,
' which gives starting or root element of that object.
' 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 variant data, which contains XML Parsing object.
' Caller : RunTestCase()
' Calls : - Nil -
'------------------------------------
Function XMLParser(sXMLFile As String, sTag As String) As Variant
Dim objXML As Variant, objNode As Variant, objRoot As Variant, objTemp As Variant
Dim iIndex As Integer, iTemp As Integer
XMLParser = NULL
sTag = Trim (sTag)
objXML = OleCreateObject ("Microsoft.XMLDOM") 'Declare a Object
OleSetProperty (objXML,"async","False")
OleDispatch (objXML, "load", sXMLFile) 'Load xmlformat file
'OleSetProperty(objXML,"preserveWhiteSpace") = False
If (OleGetProperty(OleDispatch(objXML, "parseError"), "errorCode") <> 0) Then
LogWrite ("Parsing Error in the File : " + sXMLFile, 1)
Exit Function
End If
'Top Node of given file
objRoot = OleDispatch (objXML,"documentElement")
Print OleGetProperty(objRoot, "nodeName") + " Name of the node.."
IF ((sTag = "") OR (sTag = OleGetProperty(objRoot, "nodeName"))) Then
XMLParser = objRoot
Exit Function
End IF
If OleDispatch (objRoot ,"hasChildNodes") <> 0 Then
objTemp = OleDispatch (objRoot, "ChildNodes")
iTemp = OleGetProperty(objTemp, "Length")
For iIndex = 0 To iTemp -1
objNode= OleDispatch(objTemp, "item",iIndex)
If (Ucase(trim(OleGetProperty(objNode,"nodeName"))) = UCase(sTag)) Then
XMLParser = objNode
Exit Function
Else
OleReleaseObject (objNode)
End If
Next iIndex
End IF
objTemp = OleDispatch(objXML,"getElementsByTagName",sTag) 'For xml Format
iTemp=OleGetProperty(objTemp,"Length")
If (iTemp > 0 ) Then
objNode = OleDispatch (objTemp, "item", 0)
XMLParser = objNode
End IF
End Function
No comments:
Post a Comment