Wednesday, December 2, 2009

Working With DotNetFactory Object using QTP


The below mentioned code is developed for Working With DotNetFactory Object using QTP

'Creating a DotNetFactory object pointing to a XML File
Set xmlDNFObj = DotNetFactory.CreateInstance("System.Xml.XPath.XPathDocument",, "c:\plant_catalogue.xml")

'Reading the nodes of the XML file into a list object
set xmlDNFNodes = xmlDNFObj.CreateNavigator.select( "/CATALOG/PLANT/COMMON")

'Traversing through the list of objects and displaying the values
Do While xmlDNFNodes.MoveNext

print xmlDNFNodes.current.value




loop

'Destroying the list of DotNetFactory objects
Set xmlDNFObj = Nothing


To download the XML file associated with the above program please



download plant_catalog.xml from W3School

Working With XMLUtil Object using QTP

The below mentioned code is developed for Working With XMLUtil Object using QTP

'Create a XMLUtil Object
Set xmlUtilObj = XMLUtil.CreateXML

'Load a XML file using XMLUtil Object
xmlUtilObj.LoadFile "c:\plant_catalogue.xml"

msgbox xmlUtilObj.GetRootElement()

' Get the Child elements of XML File
Set xmlChildList = xmlUtilObj.ChildElementsByPath( "/CATALOG/PLANT/COMMON")

' Traverse through the XML File to read ans display the contents
For i = 1 to xmlChildList.count
print xmlChildList.item(i).value
Next

'Destroy the XMLUtil Object
Set xmlUtilObj = Nothing


download plant_catalog.xml from W3Schools

Working With Microsoft XMLDOM using QTP

The following programme prints the values of XML file to QTP output window.

'CREATE THE COM OBJECT FOR XML USING Microsoft XMLDOM
Set xmlDOMObj = CreateObject("Microsoft.XMLDOM")

'Load XML file using
xmlDOMObj.Load("C:\plant_catalogue.xml")

'Assign PLAT nodes to a list nodes
Set nodes = xmlDOMObj.SelectNodes("/CATALOG/PLANT")
MsgBox "Total No. of Plants: " & nodes.Length

' GET ALL common NAMES of the plant
Set nodes = xmlDOMObj.SelectNodes("/CATALOG/PLANT/COMMON/text()")

' DISPLAY all the common name values
For i = 0 To (nodes.Length - 1)
NodeTitle = nodes(i).NodeValue
print "Plant Title #" & (i + 1) & ": " & NodeTitle
Next


download plant_catalog.xml from W3Schools