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 … 😀

Help file from XML Documentation using NDoc

In this article we will explore the usage of Open Source tool NDoc to create
MSDN type help file from the xml documentation in a DotNet assembly.

To know how we can create XML documentation visit the link
XML Documentation in Visual Studio using GhostDoc

Ndoc basically uses two sources to generate documentation.
One is assembly file and second is pre generated XML documentation file which we can generate Visual studio in the following way:
Go to the options in Visual studio and there we have an option to specify the path where the studio will place the file.

Now Ndoc simple exe which takes the path of Assembly and the Xml document file as generated in prevoius step and it automatically creates a CHM or web bases help file.

kick it on DotNetKicks.com

Shout it

pimp it

XML Documentation in Visual Studio using GhostDoc

In this article we will explore how to generate the xml documentation in Visual Studio for our .NET code.

Basically Every .NET assembly can contain the comments as documentation which we place after ‘///'(Triple slash) in our code.

For example:
If we write the following code in visual studio it gets assigned into assembly as xml documentation:

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        ///XML Comment
        ///Can be used to generate a document 
        ///using tools like NDoc
     }
     

Now there exists a wonderful tool GhostDoc which can automate this documentation for you. It basically extracts the documentation from the documentation of the original classes embeds it into the code.

The documentation for the page load method in the above example changed with the help of GhostDoc like this:

/// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    /// *************************************************************
    /// Created By: Ankit Goyal
    /// Date: 5/22/2009
    /// Time: 12:06 PM
    /// *************************************************************
    protected void Page_Load(object sender, EventArgs e)
    {   
    }
    

The quality of documentation which GhostDoc generates also depends on the names of classes method and variables you create. Conventions if followed along with GhostDoc produces superb documentation at ease.

Now once the xml documention has been embedded into the code it can be used to create msdn type help file for the code. To know about it please visit:
Help file from XML Documentation using NDoc

References:
Ghostdoc Home Page

kick it on DotNetKicks.com

Shout it

pimp it