Saturday, January 30, 2010

XMLUtil - How to compare two XML files.

XMLUtil-7: How to compare two XML files.

Below are the two xml files used. There is very small difference between these two XML files. Just try to understand how Compare method gives results. You can always change/modify the code according to your requirements.

XML file-1 - env.xml


XML file-2 - env1.xml


Using different XML files:

Set doc=XMLUtil.CreateXML
doc.LoadFile "C:\ENV.xml"

Set docx=XMLUtil.CreateXML
docx.LoadFile "C:\ENV1.xml"

res = docx.Compare(doc,resultDoc,micXMLValues+micXMLCDataSections)
msgbox resultDoc
if res = 1 then
msgbox "Documents match :)"
else
msgbox "do not match "
end if

Results of running the above code:

 

  


Using same XML files:

Set doc=XMLUtil.CreateXML
doc.LoadFile "C:\ENV.xml"

Set docx=XMLUtil.CreateXML
docx.LoadFile "C:\ENV.xml"

res = docx.Compare(doc,resultDoc,micXMLValues+micXMLCDataSections)
msgbox resultDoc
if res = 1 then
msgbox "Documents match :)"
else
msgbox "do not match "
end if

Results of running the above code:

  

 

Compare Method

XMLData.Compare(XMLDocument, ResultXMLDocument [, Filter])

XMLDocument: The XML document object that you want to compare to this XMLData object.

ResultXMLDocument: An XMLData object containing the differences between the XMLData object and the XML document specified in the XMLDocument argument.

Filter:
0 or micXMLNone: Compares the elements and document type declaration of the specified XML documents.
1 or micXMLAttributes: Compares the attributes of the specified XML documents, in addition to their elements and document type declaration. etc. for more on this see QTP Guide

This method returns a boolean value indicating whether or not the two files are equal.

Also See:
How to find total number of child elements.
How to view ALL elements in a message box, one at a time.
How to view the VALUE of elements in a message box, one at a time.
How to get the NAME of the root element, get the COUNT of child
elements of root, get COUNT & NAMES of children of any child element of root.

How to create a new XML file.
How to move/transfer text from XML file to QTP DataTable.