1

I'm using xmlunit for the first time to compare 2 pieces of xml. It shows great promise but has failed at the first hurdle. It is comparing two almost identical pieces of xml and claims that they are different.

Diff diff = new Diff(control, test);
diff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener()); 

The result returned by xmlunit is as follows:

[different] Expected number of child nodes '3' but was '2' - comparing <SOAP-ENV:Envelope...> at /Envelope[1] to <SOAP-ENV:Envelope...> at /Envelope[1]

But the xml is pretty much the same. Here's the control:

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope"> <SOAP-ENV:Body> <v:messagegroup xmlns:v="http://www.outfit.net/chargingandpayments/message/1.0"> <v:request> <v:msgcontrol> <v:country>GB</v:country> <v:caller> <v:name>CORE</v:name> <v:signature>Signature</v:signature> <v:version>v10</v:version> </v:caller> <v:headers/> </v:msgcontrol> <v:validate> <v:accountId>MSISDN</v:accountId> </v:validate> </v:request> </v:messagegroup> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

and here's the test String:

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope"> <SOAP-ENV:Body> <v:messagegroup xmlns:v="http://www.outfit.net/chargingandpayments/message/1.0"> <v:request> <v:msgcontrol> <v:country>GB</v:country> <v:caller> <v:name>CORE</v:name> <v:signature>Signature</v:signature> <v:version>v10</v:version> </v:caller> <v:headers /> </v:msgcontrol> <v:validate> <v:accountId>lblabla</v:accountId> </v:validate> </v:request> </v:messagegroup> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

What am I doing wrong?

mdarwin
  • 1,684
  • 7
  • 28
  • 72

3 Answers3

1

The difference is the number of children of SOAP-ENV:Envelope where XMLUnit sees either two or three children. I only see a single "real" child, so the rest likely consists of element content whitespace.

XMLUnit.setIgnoreWhitespace(true);

before evaluating the difference should fix that.

Stefan Bodewig
  • 3,260
  • 15
  • 22
0

You may need to relax the rules for comparing nodes with XMLUnit:

XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
JamesB
  • 7,774
  • 2
  • 22
  • 21
  • after applying these changes, the results were the same. "Expected number of child nodes '3' but was '2' " – mdarwin Apr 30 '14 at 13:17
0

The value of <v:accountId></v:accountId> is not the same for the two xmls.

First one is <v:accountId>MSISDN</v:accountId> and second one is <v:accountId>lblabla</v:accountId>

Mouna
  • 3,221
  • 3
  • 27
  • 38
  • 1
    firstly, I've told it to ignore the content, and secondly, even if they are the same, it still fails with the same message - which relates to the number of child nodes, not the content. – mdarwin Apr 30 '14 at 13:04
  • With the same value it works for me. I use `XMLUnit.setNormalizeWhitespace(Boolean.TRUE);` and `XMLUnit.setIgnoreComments(Boolean.TRUE);` – Mouna Apr 30 '14 at 13:09
  • I think the problem is from your `IgnoreTextAndAttributeValuesDifferenceListener`. – Mouna Apr 30 '14 at 13:21
  • Based on the name, I assume that a 'IgnoreTextAndAttributeValuesDifferenceListener' should ignore text and attribute values when comparing 2 xml documents. Either it's poorly named, or it's not working. – mdarwin May 02 '14 at 09:15
  • `IgnoreTextAndAttributeValuesDifferenceListener` ignores the content. Your difference is about the number of child nodes, not their content. The extra child node you see could be text nodes of "element content whitespace" which should vanish when you set `setIgnoreWhitespace(true)`. I don't see the difference when I paste your examples into a simple test after setting the property. – Stefan Bodewig Feb 08 '15 at 10:42