
There are many ways to compare 2 XML in Java, however we will see here one of the process that I follow. I have split this in two seperate parts. The Part1 is about how to use the APIs in order to compare the XML and Part2 would explain how to utilize that and create a SWT/Eclipse RCP Application to visually represent the differences.
I am using XMLUnit to compare two XML. I found that it’s one of the best usable API that can be pluged into the code easily. Lets see how this API works.
Download XMLUnit:
You can go to http://www.xmlunit.org/ and download the latest JAR for Java. I am using the 1.6 version since at this the version 2.0 is still in development. I got it from maven repository, here is the link http://mvnrepository.com/artifact/xmlunit/xmlunit
Configuration:
You can configure the initial details using the static methods. XMLUnit provies few functions for setting the Ignore XML Attribute, Comments, whitespace etc. For our use we will set all the following options to true.
1 2 3 4 5 |
XMLUnit.setIgnoreAttributeOrder(true); XMLUnit.setIgnoreComments(true); XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); XMLUnit.setIgnoreWhitespace(true); XMLUnit.setNormalizeWhitespace(true); |
Code:
Here is the remaining code base :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
String strGoldVersion=FileUtils.readFileToString(new File("GoldCopy.xml")); String strTestVersion=FileUtils.readFileToString(new File("TestCopy.xml")); Diff myDiff=null; DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener(); try { myDiff = new Diff(strGoldCopy,strTestCopy); myDiff.overrideDifferenceListener(myDifferenceListener); } catch (SAXException | IOException e) { e.printStackTrace(); } DetailedDiff myDiffd = new DetailedDiff(myDiff); List allDifferences = myDiffd.getAllDifferences(); for(Difference diff:allDifferences){ System.out.print(diff.getDescription()+" "); System.out.print(diff.getId()+" "); System.out.print(diff.getControlNodeDetail().getXpathLocation()+" "); System.out.print(diff.getTestNodeDetail().getXpathLocation()+" "); System.out.println(" "); } |
Test:
You can read through the code, its easy to understand. Now, to test this I have created 2 XML files.
1 2 3 4 5 6 |
<note> <heading>Reminder</heading> <to>Tove</to> <from>Jane</from> <body>Dinner!</body> </note> |
The following TestCopy.xml would be compared against the GoldCopy.xml.
1 2 3 4 5 6 |
<note> <heading>Reminder</heading> <cc>Tove</cc> <from>John</from> <body>Dinner!</body> </note> |
I ran the program and here is the result.
1 2 |
element tag name 10 /note[1]/to[1] /note[1]/cc[1] text value 14 /note[1]/from[1]/text()[1] /note[1]/from[1]/text()[1] |
You can see in the result that the element tag & text difference was printed in the command prompt. Here is the screenprint of the application we will be creating using this.The error(element difference) will be highlited in RED and TEXT difference in GREEN.
Hi, I need your help in the code. I am getting “List cannot be resolved to a type” when I copied the code from http://www.adeveloperdiary.com/java/xml/compare-2-xml-in-java-part-1/.
I tried importing different import and still had issues. Please advise.
List allDifferences = myDiffd.getAllDifferences();
System.out.println(“Difference size: ” + allDifferences.size());
for (int i = 0; i < allDifferences.size(); i++) {
Difference diff = (Difference) allDifferences.get(i);
System.out.print(diff.getDescription() + " ");
System.out.print(diff.getId() + " ");
System.out.print(diff.getControlNodeDetail().getXpathLocation() + " ");
System.out.print(diff.getTestNodeDetail().getXpathLocation() + " ");
System.out.println(" ");
}
You can use List allDifferences = myDiffd.getAllDifferences(); instead also
Use List(Difference) allDifferences = myDiffd.getAllDifferences();
Replace ‘(‘ with less than and ‘)’ with greater than symbols
Hi,
Can you please share me the complete source code to compare XML files.
Thanks and Regards,
Preethi
Hi
Can you please share me the complete source code to compare XML files.
Thanks and Regards,
Rakesh
Can you pls. give me the link for Part2