timmy
timmy

Reputation: 1758

XmlUnit and Child Nodes

I am using xmlunit to compare two text files. The control xml is:

<books>
    <book>
       <name>Angels &amp; Demons</name>
       <isbn>9971-5-0210-0</isbn>
       <author>Dan Brown</author>
       <category></category>
    </book>
</books>

I am comparing this against another piece of xml that has the and elements swapped.

<books>
    <book>
        <isbn>9971-5-0210-0</isbn>
        <name>Angels &amp; Demons</name>
        <author>Dan Brown</author>
        <category></category>
    </book>
</books>

The Diff object reports the following difference:

Expected sequence of child nodes '1' but was '3' - comparing <name...> at /books[1]/book[1]/name[1] to <name...> at /books[1]/book[1]/name[1]

If <name> is child node '1', wouldn't <isbn> be child node '2'?

Upvotes: 1

Views: 2201

Answers (2)

user2904470
user2904470

Reputation: 11

For xmlUnit 2.X Version:

For the xmlUnit 2.xx version, the XMLUnit.setIgnoreWhitespace(true) is not anymore applicable. Now, you can add the "ignoring whitespace" directly to the DiffBuilder by adding DiffBuilder.ignoreWhitespace().

Diff diffXml =DiffBuilder.compare(expectedXml).withTest(actualXml).normalizeWhitespace().checkForSimilar().build();

for asserting the xmls are similar you can eg. do:

   MatcherAssert.assertThat(diffXml.toString(), is("[identical]"));

For furhter info about changes between 1.x to 2.x see: https://github.com/xmlunit/user-guide/wiki/Migrating-from-XMLUnit-1.x-to-2.x

Upvotes: 1

timmy
timmy

Reputation: 1758

It seems like XmlUnit is counting carriage returns in the xml as child nodes. Setting the following XMLUnit.setIgnoreWhitespace(true); gave a more intuitive result of Expected sequence of child nodes '0' but was '1' - comparing <int...> at /struct[1]/int[1] to <int...> at /struct[1]/int[1]

Upvotes: 0

Related Questions