How to compare two xml files using CreateXMLfromFile Method

In order to compare two xml files in QTP we use the "XMLUtil Object" which is used to access and return XML objects.The Associated Methods for this object is CreateXML Method & CreateXMLfromFile Method.



1. CreateXML Method: This method is used to create and return an object of the type XMLData. If a root name is specified i.e path of the xml file, a new document is created containing the specified root tag.

2. CreateXMLFromFile Method: This method is used to create and return an object of type XMLData

Example: Below is an example which creates an XML object and uses it to load an XML file.

CreateXML Method
Set XMLObj = XMLUtil.CreateXML()
XMLObj.LoadFile("E:\Environment.xml")

Using this method we will write a script to compare two xml files and return the result in a result.xml file.
'assigining the path of the xml file to a variable

xmlFile1 = "E:\Perspectives_FrameWork\Environment.xml"
xmlFile2 = "E:\Perspectives_FrameWork\Environment 1.xml"

'Creates and returns an object of type XMLData

Set objxml1 = XMLUtil.CreateXML()
Set objxml2 = XMLUtil.CreateXML()

'Loading the c=xml file

objxml1.LoadFile (xmlFile1)
objxml2.LoadFile (xmlFile2)

' Comparing the two xml files and setting the value either 1 or 0 to the variable
Result = objXML1.compare(objXML2, objResultsXML)

If Result = 1 Then
MsgBox "XML Files Match"
Else
MsgBox "XML Files do not match"

'saving the result xml file

objResultsXML.savefile ("E:\results.xml")

End If

CreateXMLfromFile Method

This method is used to create and return an object of type XMLData

Example: Below is the example given which creates an XML object and loads the XML file Environment.xml into it.

Set XMLObj = XMLUtil.CreateXMLFromFile("E:\Environment.xml")

Below is the script to compare two xml files named Environment and Environment 1.
' using a CreateXMLfromFile Method creates an XML object and loads the XML file specified into it

Set objxml1 = XMLUtil.CreateXMLFromFile("E:\Perspectives_FrameWork\Environment.xml")
Set objxml2 = XMLUtil.CreateXMLFromFile("E:\Perspectives_FrameWork\Environment 1.xml")

' Comparing the two xml files and setting the value either 1 or 0 to the variable

Result = objXML1.compare(objXML2, objResultsXML)

If Result = 1 Then
Msgbox "XML Files Match"
Else
Msgbox "XML Files do not match"
objResultsXML.savefile ("E:\results.xml")
End If

4 comments:

Anonymous said...

thanks for the code..ve been searching to solve this

Anonymous said...

Hi,

Thanks for providing such an easiest way of xml compare.

Still I need your help in running this code without using QTP. When itried to run this code through vbs file, I am getting a run time error as XML Util is a qtp build in method.

Please provide me a solution in this case also if possible guide me how I can create XML Util object without using qtp.

Thanks in advance..

~Venu

hiszpanski said...

Hi, how to skip specific fields in XML when comparing 2 xmls?
for example I would like to ignore any change under "Cars" field.

Please help.

Healthandfigure said...

ajj kal log apne sexlife se kaafi presaan rahte hai uski wajah hai time se phle hi josh khatam ho jaana, choti panies ye sb wajah se ajj kal ke larke apne partner or wife ko khus nahi kar paate hai
Time badhane ki medicine patanjali
jiske wajah se unka relation jyada din nahi chalata hai or bhut jaldi unki relation khatam ho jaati hai

Post a Comment

/* Tynt Insight tracker ----------------------------------------------- */