5

I want 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.

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.

Rob Kielty
  • 7,958
  • 8
  • 39
  • 51
Ahmed
  • 51
  • 1
  • 1
  • 2

3 Answers3

8

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
  • 4,269
  • 3
  • 27
  • 22
  • 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 Nov 13 '09 at 00:56
  • How big is your XML file? Could you paste the content here? – Rohit Agarwal Nov 13 '09 at 17:13
  • How to handle if any tag is missing say for example, from controlXML id tag is missing String controlXML = "acme"; String testXML = "acme3A-00"; if there are more than 3 child node then comparison showing wired result – Pushkar Oct 29 '14 at 05:58
3

This should do it:

    // Assuming file1 and file2 are not deeply nested XML files
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc1 = docBuilder.parse(file1);
    Document doc2 = docBuilder.parse(file2);

    // SOLUTION 1: Are the files "similar"?
    Diff diff = new Diff(doc1, doc2);
    System.out.println("Similar (true/false): " + diff.similar());

    // SOLUTION 2: Should you want detailed differences (especially useful for deeply nested files)
    Diff diff = new Diff(doc1, doc2);
    diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); 
    DetailedDiff detailedDiff = new DetailedDiff(diff); 
    System.out.println("Detailed differences: " + detailedDiff.getAllDifferences().toString());

Hope that helps a bit. Read up on XMLUnit here.

Rubicon
  • 137
  • 7
0
diff =
        DiffBuilder.compare(expected)
          .withTest(toBeVerified)
          .ignoreWhitespace()
          .checkForSimilar()
          .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText, ElementSelectors.byName))
          .build();
m4n0
  • 29,823
  • 27
  • 76
  • 89
Sanje
  • 1