Search code examples
javaxmlxsdcomparexmlunit

XMLunit Comparison failure because child nodes order


I'm comparing a generated XML file with another example using XMLunit, and Im having problems with ChildNodes Order and their attributes. (Linux and Mac generation differ)

This is what I've tried:

@Test
public void testComparingXML() throws Exception {
    XMLUnit.setIgnoreWhitespace(true);
    String expectedXml = IOUtils.toString(ConnectorStrategyStudioXmlTest.class.getClassLoader().getResourceAsStream(EXPECTED_XML));
    String actualXml = IOUtils.toString(ConnectorStrategyStudioXmlTest.class.getClassLoader().getResourceAsStream(ACTUAL_XML));
    Diff diff = new Diff(expectedXml, actualXml);
    diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
    DetailedDiff detailedDiff = new DetailedDiff(diff);
    assertTrue(detailedDiff.toString(), detailedDiff.similar());
}

I've read that using RecursiveElementNameAndTextQualifier class could resolve the problem, but still doesn't work.

Here is an image with an example of the XML comparison failure: inverted (open in a new tab for full screen) c:

As you can see, both child nodes are inverted

TY in advance. Juan


Solution

  • Using a non-default ElementQualifier is the correct solution, but RecursiveElementAndTextQualifier certainly doesn't do what you need.

    At first glance it looks as if it should be possible to find the matching elements by looking at the element's name and the value of the caption attribute. If this is correct, then ElementNameAndAttributeQualifier with "caption" passed in as constructor argument should do the trick.

    There are more options built into XMLUnit, in the worst case you'd need to implement the ElementQualifier interface yourself if none of the existing options fits your needs.