Xml Parsing and Comparison

424 views Asked by At

I have two Xml:-

String xmlA="<user><name>Jai</name><age>31</age><dob>28March1990</dob></user>";
String xmlB="<user><name>Jai Singh</name><age>31</age><dob>28March1990</dob></user>";

Now After parsing and comparing, I want to show the results in following order:-

Name : Jai JaiSingh

Age: 31 31

dob: 28March1990 28March1990

My question: What is the easiest method to approach this?

Can I use xmlUnit for comparing in this scenario?

2

There are 2 answers

0
Jayan On BEST ANSWER

Using xmlunit

Create two xml documents and compare using Diff. The DifferenceEngine accepts a DifferenceListener. You can add your logic there.

        Diff diff = new Diff(docx1, docx2);
        DifferenceEngine engine = new DifferenceEngine(diff);

        ElementQualifier qualifier = new RecursiveElementNameAndTextQualifier();
        diff = new Diff(docx1, docx2, engine, qualifier);
        diff.overrideDifferenceListener(new DifferenceListener()
        {
            @Override public int differenceFound(Difference difference)
            {
                // Do some thing here


            }

            @Override public void skippedComparison(Node node, Node node1)
            {
                //no op
            }
        });
1
Elorry On

Of course, you have a XML Unit if you want to compare two XML and you could do it character by character. XML Unit

Edited:

You have to download de XMLUnit JAR from here XML Sourceforge and then its easy, like JUnit, add to your classpath (by Eclipse / NetBeans / so on ...) and then you write a Test, like JUnit but extends from XMLTestCase, like this one:

import org.custommonkey.xmlunit.*;
public class XMLTesting extends XMLTestCase {


 public void testForEquality() throws Exception {
        String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";
        String myTestXML = "<msg><localId>2376</localId></msg>";
        assertXMLEqual("comparing test xml to control xml", myControlXML, myTestXML);

        assertXMLNotEqual("test xml not similar to control xml", myControlXML, myTestXML);
    }

    public void testIdentical() throws Exception {
        String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";
        String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";
        Diff myDiff = new Diff(myControlXML, myTestXML);
        assertTrue("pieces of XML are similar " + myDiff, myDiff.similar());
        assertTrue("but are they identical? " + myDiff, myDiff.identical());
    }

    public void testAllDifferences() throws Exception {
        String myControlXML = "<news><item id=\"1\">War</item>"
            + "<item id=\"2\">Plague</item><item id=\"3\">Famine</item></news>";
        String myTestXML = "<news><item id=\"1\">Peace</item>"
            + "<item id=\"2\">Health</item><item id=\"3\">Plenty</item></news>";
        DetailedDiff myDiff = new DetailedDiff(compareXML(myControlXML, myTestXML));
        List allDifferences = myDiff.getAllDifferences();
        assertEquals(myDiff.toString(), 0, allDifferences.size());
    }

So you can see the easy way to compare two XML