javaxmlxmlunit

Comparing XML equal files (except for whitespace) with xmlunit produces difference


Using xmlunit (version 2.6.0) to compare two XML files produces differences when they should be considered equal.

XML 1:

<top><a>one</a><b>two</b></top>

XML 2:

<top>
  <a>one</a>
  <b>two</b>
</top>

Java code: Source xmlSource1 = Input.fromFile(xmlFile1).build(); Source xmlSource2 = Input.fromFile(xmlFile2).build();

DefaultNodeMatcher nodeMatcher = new DefaultNodeMatcher(ElementSelectors.byNameAndText);

Diff d = DiffBuilder.compare(xmlSource1)
            .withNodeMatcher(nodeMatcher)
            .withTest(xmlSource2).build();
    Iterable<Difference> diffList = d.getDifferences();
    Iterator<Difference> iterator = diffList.iterator();
    while(iterator.hasNext()) {
        Difference next = iterator.next();
        log.info("Difference: " + next);
    }

Produces this output:

Difference: Expected child 'top' but was 'null' - comparing <top...> at /top[1] to <NULL> (DIFFERENT)
Difference: Expected child 'null' but was 'top' - comparing <NULL> to <top...> at /top[1] (DIFFERENT)

Question: why are they considered different? How can this comparison be done by ignoring whitespace differences? Ideally I would like d.hasDifferences() to be false.


Solution

  • Just ignore whitespaces (and comments) and perform check where similarities (see checkForSimilar()) are not included in the diff:

    Diff d = DiffBuilder.compare(xmlSource1).withTest(xmlSource2)
     .checkForSimilar()
     .withNodeMatcher(nodeMatcher)
     .ignoreWhitespace()
     .ignoreComments()
     .build();