Wednesday, December 5, 2012

XMLUtil - Part 2

I request people watch XMLUtil - Part 1, before watching this video.

http://qtpftvideos.blogspot.in/2012/12/how-to-work-with-xml-files-part-1.html

In this video i demonstrated more about how to update an XML File.
How to update XML Elements.
How to add XML attributes to an XML Element.
How to update XML attribute values.
How to add child and sub-child elements.


'***************************************************************************************************************************************************************************************
'Objective              :  Cover basic XML Operations
'Functionality        : The below script  covers the updating off XML Files
'                                  
'Author                    : Udaya Anem
'Scripted on       : 4th Dec
'Updated by         :
'***************************************************************************************************************************************************************************************
Dim o_XML
Dim s_XMLFilePath

s_XMLFilePath="E:\Programming Samples\QTP Samples\Cars.xml"
s_UpdatedXMLFilePath="E:\Programming Samples\QTP Samples\Cars2.xml"

'Set o_XML=XMLUtil.CreateXMLFromFile(s_XMLFilePath)
Set o_XML=XMLUtil.CreateXMLFromFile(s_XMLFilePath)

'The below steps update specified XML Element value
Set o_ChildPath=o_XML.ChildElementsByPath("/Cars/Make/Model/Name")
print o_ChildPath.Item(1).Value
o_ChildPath.Item(1).SetValue("Swift")
o_XML.SaveFile s_UpdatedXMLFilePath
print o_ChildPath.Item(1).Value

'The below script adds an attribute to the XML Element
Set o_ChildPath=o_XML.ChildElementsByPath("/Cars/Make/Model/Name")
o_ChildPath.Item(1).AddAttribute "Version","Petrol"
o_XML.SaveFile s_UpdatedXMLFilePath
''
''
set o_XMLAttributes=o_ChildPath.Item(1).Attributes
print o_XMLAttributes.Item(1).Value

'to update an attribute also we have to use AddAttribute
o_ChildPath.Item(1).AddAttribute "Version","Diesel"
o_XML.SaveFile s_UpdatedXMLFilePath

set o_RootElement=o_XML.GetRootElement
o_RootElement.AddChildElementByName "Make",""
Set o_ChildPath=o_XML.ChildElementsByPath("/Cars/Make")
o_ChildPath.Item(o_ChildPath.Count).AddAttribute "name","Audi"
o_ChildPath.Item(o_ChildPath.Count).AddChildElementByName "Model",""
Set o_ChildPat=o_XML.ChildElementsByPath("/Cars/Make/Model")
o_ChildPat.Item(o_ChildPat.Count).AddChildElementByName "Name","A4"
o_ChildPat.Item(o_ChildPat.Count).AddChildElementByName "Release","2002"
o_XML.SaveFile s_UpdatedXMLFilePath

Set o_ChildPath=nothing
Set o_XML=nothing

No comments:

Post a Comment