A Developer Diary

{about:"code learn and share"}

  • Home
  • Data Science
    • Machine Learning
  • Java
    • Cache
    • WebService
      • REST
      • SAAJ
    • Server
    • Spring Boot
    • XML
  • JavaScript
    • Angular JS
      • Angular 1.x
      • Angular 2.x
    • D3.js
    • React JS
  • jBPM
  • Tools
    • Yeoman
  • Tips
  • About

June 9, 2015 By Abhisek Jana

Compare 2 XML in JAVA – Part 1

Compare 2 XML in JAVA – Part 1
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this pageShare on TumblrShare on Reddit

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.

Java
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 :

Java
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.

GoldCopy.xml
XHTML
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.

TestCopy.xml
XHTML
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.

XML Compare adeveloperdiary

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this pageShare on TumblrShare on Reddit

Related

Filed Under: XML Tagged With: Compare, Java, XML, XMLUnit

Comments

  1. Vinay S Reddy says

    September 12, 2016 at 9:20 am

    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.

    • SFSZ says

      December 19, 2016 at 5:51 pm

      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(" ");
      }

    • Anil says

      December 30, 2016 at 1:05 pm

      You can use List allDifferences = myDiffd.getAllDifferences(); instead also

      • Anil says

        December 30, 2016 at 1:07 pm

        Use List(Difference) allDifferences = myDiffd.getAllDifferences();

        Replace ‘(‘ with less than and ‘)’ with greater than symbols

  2. Preethi says

    February 14, 2017 at 8:25 am

    Hi,

    Can you please share me the complete source code to compare XML files.

    Thanks and Regards,
    Preethi

    • Rakesh says

      May 2, 2017 at 7:08 am

      Hi

      Can you please share me the complete source code to compare XML files.

      Thanks and Regards,
      Rakesh

  3. Krishna says

    September 27, 2017 at 12:16 pm

    Can you pls. give me the link for Part2

Top Posts

  • How to Create Spring Boot Application Step by Step 158,738 views | 9 comments
  • How to create RESTFul Webservices using Spring Boot 54,113 views | 24 comments
  • How to integrate React and D3 – The right way 52,147 views | 30 comments
  • How to convert XML to JSON in Java 33,081 views | 4 comments
  • How to deploy Spring Boot application in IBM Liberty and WAS 8.5 32,002 views | 8 comments
  • Get started with jBPM KIE and Drools Workbench – Part 1 29,784 views | 14 comments
  • An Introduction to Spring Boot 28,070 views | 9 comments
  • How to easily encrypt and decrypt text in Java 24,637 views | 8 comments
  • How to Create Stacked Bar Chart using d3.js 23,380 views | 15 comments
  • Create a simple Donut Chart using D3.js 20,468 views | 5 comments

Search in this website

Copyright © 2018 A Developer Diary