anuvab1911 anuvab1911 - 6 months ago 34
Java Question

XMLUnit - How to compare a self-closing tag to one which isn't?

I have two pieces of XML, a part of which is :

<userActionRequiredCode>0</userActionRequiredCode>

<userActionRequiredCode xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>


These are in different files which I am comparing using XMLUnit.

These are different as you can see, not same, not similar. What options can I set for XMLUnit or the Diff class or anywhere so that I can take this difference into consideration.

Currently this doesn't show up in the list of differences.

Edit : Updated sample.

Answer

A simple MCVE shows them to be different, so if you don't get a difference, then you setup XMLUnit to not do so.

final String control = "<userActionRequiredCode>0</userActionRequiredCode>";
final String test = "<userActionRequiredCode xsi:nil=\"1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>";

Diff myDiff = DiffBuilder.compare(Input.fromString(control))
                         .withTest(Input.fromString(test))
                         .build();
System.out.println(myDiff.toString());

OUTPUT

Expected child nodelist length '1' but was '0' - comparing <userActionRequiredCode...> at /userActionRequiredCode[1] to <userActionRequiredCode...> at /userActionRequiredCode[1]