Labels

Friday 5 October 2012

QTP - XML Handling


'' XML

Set xmlDoc = XMLUtil.CreateXMLFromFile("C:\data\QTP scripts\test.xml")
'' Convert the xml into string
sXML = xmlDoc.ToString
print sXML

' create another  copy of the xml
Set dXML = XMLUtil.CreateXML()
dXML.Load sXML
dXML.SaveFile ("C:\data\QTP scripts\test1.xml")

'Exporting XML into a dictionary
Set xmlRoot = xmlDoc.GetRootElement()
Set xmlValues = xmlRoot.ChildElementsByPath("/xml/Environment")

For i=1 To xmlValues.Count
Set xmlElement = xmlValues.Item(i)
strName = xmlElement.ChildElementsByPath("Name").item(1).value
strVersion = xmlElement.ChildElementsByPath("Version").item(1).value
strDB = xmlElement.ChildElementsByPath("DBName").item(1).value
print strName & strVersion & strDB
Next

''Playing with dictionary object
Set dicObj = CreateObject("Scripting.Dictionary")
dicObj.add  "a","sundar"
dicObj.add "b","durga"

print "No of items in the dic obj:::" & dicObj.Count

For each key in dicObj.Keys
print key
Next

For each key in dicObj.items
print key
Next

dicObj.RemoveAll

If xmlValues.Count  <> 0 Then
Set xmlElement = xmlValues.Item(1)
iCount = xmlElement.ChildElements.Count()
print iCount
For i= 1 To iCount
strName = xmlElement.ChildElements.item(i).ElementName()
strNameVal=xmlElement.ChildElements.item(i).Value()
print strName & strNameVal
dicObj.add strName, strNameVal
Next
End If

print "No of items in the dic obj:::" & dicObj.Count

For each key in dicObj.Keys
print key
Next

For each key in dicObj.items
print key
Next


'' XML file

<xml>
<Environment>
<Name>QA</Name>
<Version>1.1</Version>
<DBName>M2Q2</DBName>
</Environment>
</xml>



No comments:

Post a Comment