Showing posts with label xmlparser. Show all posts
Showing posts with label xmlparser. Show all posts

Tuesday, March 18, 2008

MS XML Parser by Visual Basic

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

Friday, March 14, 2008

VisualTest - MS XML Parser

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