tags:

views:

382

answers:

2

Hi

I am trying to use XMLUnit to compare two similar XML files. Basically every thing is same. File1 is a copy of File2. But in File2 I have changed the order of some elements in one node.

Now I am trying to run a test where it compares these files and returns a result of "similar" and not treat these files as "different".

Can you please help me? Thanks!

+2  A: 

I think this link can help you - http://www.ibm.com/developerworks/java/library/j-cq121906.html#N10158

Basically, if your File1 is like -

<account>
 <id>3A-00</id>
 <name>acme</name>
</account>

And File2 is same, but only differs in order of <name> and <id> -

<account>
 <name>acme</name>
 <id>3A-00</id>
</account>

Then you can write a test like below which will compare these and return similar.

public void testIdenticalAndSimilar() throws Exception {
   String controlXML = "<account><id>3A-00</id><name>acme</name></account>";
   String testXML = "<account><name>acme</name><id>3A-00</id></account>"; 
   Diff diff = new Diff(controlXML, testXML);
   assertTrue(diff.similar());
   assertFalse(diff.identical());
}

Hope that helps.

Rohit Agarwal
When I use a String the comparison works fine. But when I use the file it doesn't. I declare InputSource object for each file and then create a Diff object with the two InputSource objects in it. When I use the similar command in this way it fails and says that the files are different. Is this a valid way of comparing?
Ahmed
How big is your XML file? Could you paste the content here?
Rohit Agarwal
A: 

I am trying to compare these two files (note1.xml and note2.xml). My code does not return a "similar" result. What am I doing wrong? Please help.

**//////////////////////////My code** InputSource xml1 = new InputSource("C:\xmlunit-1.3\note1.xml"); InputSource xml2 = new InputSource("C:\xmlunit-1.3\note2.xml"); Diff different = XMLUnit.compareXML(xml1, xml2); System.out.println("******************************************"); System.out.println("Similar: " + different.similar() + "\nIdentical: " + different.identical() + "\nDifferent: " + different.toString()); System.out.println("*****************************************");

/////////////////////////Output


Similar: false Identical: false Different: org.custommonkey.xmlunit.Diff [not identical] Expected sequence of child nodes '1' but was '5' - comparing at /note[1]/to[1] to at /note[1]/to[1]

[not identical] Expected sequence of child nodes '3' but was '1' - comparing at /note[1]/from[1] to at /note[1]/from[1]

[different] Expected text value ' ' but was ' ' - comparing at /note[1]/text()[3] to at /note[1]/text()[3]


/////////////////////////note1.xml Tam John Reminder How are you?

//////////////////////note2.xml John Reminder Tam
I'm fine.

Sorry forgot to paste the xml files:**/////////////////////////note1.xml**<note><to>Tam</to> <from>John</from> <heading>Reminder</heading> <body>How are you?</body> </note>**//////////////////////note2.xml** <note><from>John</from> <heading>Reminder</heading><to>Tam</to> <body>I'm fine.</body> </note>
"I'm fine" and "How are you?" are NOT similar, ie. the difference between the values of the <body> nodes is not *recoverable*. That said, if all of the nodes' values were identical between the two files and the only difference was node **order** then the difference is *recoverable*, therefore similar() would return true.
belwood