Friday, January 22, 2010

Comparing two XML documents.

Example 5: Comparing two XML documents.

Below are the two sample XML files I used. See the only difference in these two files is that the file 1 has an extra node <price2>460</price2> for book category cooking.

XML File 1:




XML File 2:



Set xml_Doc1 = CreateObject("Msxml2.DOMDocument")
a=inputbox("Enter path for first file: ")
xml_Doc1.load(a)
Set xml_Doc2 = CreateObject("Msxml2.DOMDocument")
b=inputbox("Enter path for second file: ")
xml_Doc2.load(b)

Set Elem_File_1= xml_Doc1.DocumentElement.ChildNodes
Set Elem_File_2= xml_Doc2.DocumentElement.ChildNodes

if file 1 & file 2 have different child nodes then the code quits
If Elem_File_1.length=Elem_File_2.length Then
  msgbox "File 1 & File 2 have same number of Child nodes"
Else
msgbox "File 1 & File 2 have different Child nodes"
WScript.Quit
end if

For i = 0 to Elem_File_1.length-1

If Elem_File_1.item(i).Text = Elem_File_2.item(i).Text Then
  msgbox "Child Element: "& i &" is SAME in File-1 & File-2" & vbnewline & "In File-1, The value is: " & Elem_File_1.item(i).Text & vbnewline & "In
File-2, The value is: "&Elem_File_2.item(i).Text
Else
  msgbox "Child Element: "& i &" is NOT SAME in File-1 & File-2" & vbnewline & "In File-1, The value is: " & Elem_File_1.item(i).Text & vbnewline &
"In File-2, The value is: "&Elem_File_2.item(i).Text
End If

Next

Results of running the above code:



 

 

 

 

Also See:
Explain XML and XML DOM
VBScript to access number of child nodes and text/nodename of each child node from XML file.
VBScript to access node name and node type of all nodes from XML document.
VBScript to add a new node in XML document.
VBScript to append a string at the end of a text node in XML file.