0

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.

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your quick response!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.