QTP Certification Help

QTP Scripts for XML

Working with XML object from QTP
Quick Test Professional provides several scripting methods that we can use with XML data. We can use these scripting methods to retrieve data and return new XML objects from existing XML data.

XMLUtil in QTP
The object used to access and return XML objects.

Set Object = XMLUtil.CreateXML()

Loading XML file from QTP
Set XMLObj = XMLUtil.CreateXML()


Code for replacing the value in XML file using XML Object:
Set doc = XMLUtil.CreateXML()
doc.LoadFile "C:\qatutorial.xml"
Set root = doc.GetRootElement()
Set children = root.ChildElements()
if children.Count() <> 0 then
Set firstChild = children.Item(1)
firstChildName = firstChild.ElementName()
Set sameNameChildren = children.AllItemsByName(firstChildName)
msg = "My first child name is " + firstChildName + " and I have " + FormatNumber(sameNameChildren.Count(),0) + " of them."
End If
msgbox msg
set firstChild=root.ChildElements().Item(2)
Set child2 = firstChild.ChildElements().Item(2)
Msgbox child2.Value()
firstChild.AddChildElementByName "Value", "replacement"
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("C:\qatutorial.xml")
MsgBox MyFile.ReadAll