XML or String comparison tutorial using google-diff-match-patch and XMLUnit API

Requirment– To compare the two xml or string and find out the difference. I need to show the difference with color as well, if it is added then its in green otherwise it should be in red.
Different from normal ADF stuff. It’s a pure java work. Let find out, how to achieve it.We can try few open source .Following are the different xml diff libraries
http://www.java-tips.org/java-libraries/xml-diff-libraries/

DIffXML-Defines an interface for comparing two XML files. It enables two XML files to be compared to check for their equivalence. It provides the objects to display the differences, if any, in a graphical format. The differences can also be represented as XSL. The corresponding XSL stylesheet with the differences can be generated as a file or an XMLDocument object. The first XML file can be transformed into the second XML file by using the XSL stylesheet generated.
XMLDiff
http://docs.oracle.com/cd/B12037_01/appdev.101/b12024/oracle/xml/differ/XMLDiff.html
First i tried DiffXML api from oracle.I read the documentation in oracle site and I am impressed with the documentation.I tried to download the jar from oracle site. I don’t know what went wrong , I found everything but no option to download.strange . πŸ˜›

After spending few hours , I drop this idea, I tried another one api i.e XMLUNIT

XMLUNIT– XMLUnit for Java provides two JUnit extension classes, XMLAssert and XMLTestCase, and a set of supporting classes (e.g. Diff, DetailedDiff,Transform,SimpleXpathEngine,Validator,NodeTest) that allow assertions to be made about:
β€’ The differences between two pieces of XML
β€’ The outcome of transforming a piece of XML using XSLT
β€’ The evaluation of an XPath expression on a piece of XML
β€’ The validity of a piece of XML
β€’ Individual nodes in a piece of XML that are exposed by DOM Traversal

XMLUnit for Java can also treat HTML content (even badly-formed HTML) as valid XML to allow these assertions to be made about the content of web pages too. You can download the code and jar from below url – URL: http://xmlunit.sourceforge.net/

Finally ,I found the jar.I added the in the project library in build path using project properties.

I created a file called comparsionTest.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.List;
 
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.SAXException;
 
public class ComparisonTest {
 
    public static void main(String[] args) {
        URL url1 = ComparisonTest.class.getResource("comparison.xml");
        URL url2 = ComparisonTest.class.getResource("reference.xml");
        FileReader fr1 = null;
        FileReader fr2 = null;
        try {
            fr1 = new FileReader(url1.getPath());
            fr2 = new FileReader(url2.getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
        //Add init code here.
 
        try {
            Diff diff = new Diff(fr1, fr2);
            System.out.println(diff.similar());
            System.out.println(diff.identical());
 
            DetailedDiff detDiff = new DetailedDiff(diff);
            List differences = detDiff.getAllDifferences();
            for (Object object : differences) {
                Difference difference = (Difference)object;
                System.out.println("***********************");
                System.out.println(difference);
                System.out.println("***********************");
            }
 
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Add the reference and comparison.xml in resource folder of project like

Reference.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <name>Angels &amp; Demons</name>
        <isbn>9971-5-0210-0</isbn>
        <author>Dan Brown</author>
        <category></category>
    </book>
    <book>
        <name>You can win</name>
        <isbn>9971-5-0222-0</isbn>
        <author>Vinay</author>
    </book>
</books>

comparison.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <name>Angels &amp; Demons</name>
        <isbn>9971-5-0210-0</isbn>
        <author>Dan Brown</author>
        <title>I am great</title>
        <category></category>
    </book>
    <book>
        <name>You can win</name>
        <isbn>9971-5-0222-0</isbn>
        <author>Shiv Khera</author>
    </book>
</books>

Run the file as application in eclipse and see output as below

false
false
***********************
Expected number of child nodes '11' but was '9' - comparing <book...> at /books[1]/book[1] to <book...> at /books[1]/book[1]
***********************
***********************
Expected presence of child node 'title' but was 'null' - comparing <title...> at /books[1]/book[1]/title[1] to  at null
***********************
***********************
Expected text value '
        ' but was '
    ' - comparing <book ...>
        </book> at /books[1]/book[1]/text()[5] to <book ...>
    </book> at /books[1]/book[1]/text()[5]
***********************
***********************
Expected sequence of child nodes '9' but was '7' - comparing <category...> at /books[1]/book[1]/category[1] to <category...> at /books[1]/book[1]/category[1]
***********************
***********************
Expected sequence of child nodes '10' but was '8' - comparing <book ...>
    </book> at /books[1]/book[1]/text()[6] to <book ...>
    </book> at /books[1]/book[1]/text()[5]
***********************
***********************
Expected text value 'Shiv Khera' but was 'Vinay' - comparing <author ...>Shiv Khera</author> at /books[1]/book[2]/author[1]/text()[1] to <author ...>Vinay</author> at /books[1]/book[2]/author[1]/text()[1]
***********************

Detailed explanation of xmlUnit you can found on http://technicalmumbojumbo.wordpress.com/2010/01/31/xml-comparison-tutorial-using-xmlunit/

************************************************************************************************************************************************************************

google-diff-match-patch=Compare two blocks of plain text and efficiently return a list of differences.

After trying this xmlUnit, it does not solve my purpose πŸ™ .i need something else.so i found one more api google-diff-match-patch and you can
find in http://code.google.com/p/google-diff-match-patch/

create new project in eclipse and copy diff_match_patch.java from source of above code. And create your new class and implement as below

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.ListIterator;



public class LineComparsion {

public static void main(String args[]) {
	String text1 ="hello i am best. where are you";// readFile("rahul.txt");
	String text2 ="hello i am best good.i am here";
	
 compareFile(text1,text2);
System.gc();


}



private static  String compareFile(String text1,String text2) {
	
	 diff_match_patch dmp = new diff_match_patch();
	 dmp.Diff_Timeout = 0;

	 // Execute one reverse diff as a warmup.
	 LinkedList difference= dmp.diff_main(text2, text1, false);

	 ListIterator itr = difference.listIterator();

	 String abc=dmp.diff_prettyHtml(difference);
	 System.out.println(abc);
	 
	
	 
	 long start_time = System.currentTimeMillis();
	 dmp.diff_main(text1, text2, false);
	 long end_time = System.currentTimeMillis();
//	 System.out.printf("Elapsed time: %f\n", ((end_time - start_time) / 1000.0));
	return text2;
	 // Read a file from disk and return the text contents.
	
	}



private static String readFile(String filename) {
 // Read a file from disk and return the text contents.
 StringBuffer strbuf = new StringBuffer();
 try {
   FileReader input = new FileReader(filename);
   BufferedReader bufRead = new BufferedReader(input);
   String line = bufRead.readLine();
   while (line != null) {
     strbuf.append(line);
     strbuf.append('\n');
     line = bufRead.readLine();
   }
   
   bufRead.close();
  
 } catch (IOException e) {
   e.printStackTrace();
 }
 return strbuf.toString();
}
}

That all and you output in string with html tag i.e .You have use diff_prettyHtml method to get html output.there are multiple method and you can test or play with them

hello i am best good.i am where are you

If you run as html you see as below

This is really great and what I wanted. So I can say google-diff-match-patch is best api to compare string or xml with great output.

Happy coding with techartifact … πŸ˜€

Vinay

I am an Oracle ACE in Oracle ADF/Webcenter. Sr Java Consultant-working on Java/J2EE/Oracle ADF/Webcenter Portal/ content and Hibernate for several years. I'm an active member of the OTN JDeveloper/Webcenter forum. Passionate about learning new technologies. I am here to share my knowledge. Give your views and suggestion on [email protected] .

More Posts - Website

Follow Me:
TwitterLinkedInGoogle PlusYouTube