Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLUnit - Xml file indentation impacts on comparison

I am currently trying to use the XMLUnit library to compare two XML files. One of them, the candidate, is generated by my code from Java Objects (using JAXB) and the other one is the reference (I cannot modify it). Basically I am trying to prove that given a reference XML file I can unserialize it (using Jaxb and some classes of my own) then serialize it back to another file and still have the same content.

The library seems to furnish the services I need but when the generated file is not properly indented (in kind of a "pretty-print" version) the comparison fails and it doesn't when indentation is OK. For example when the candidate is generated there is no indentation, the content is a one-liner, if a indent it properly (manually) the comparison is OK.

Here is the error message generated by XMLUnit:

[different] Expected number of child nodes '3' but was '1'

Do you guys have any idea to solve this? Maybe the solution is to generate a pretty-print version of the candidate, in this case do you have an idea to combine it with the JAXB serialiser?

By the way if you now a better solution in Java to compare XML files I'll be glad to know it ;)

Thanks in advance for your help.

like image 325
reef Avatar asked Mar 09 '11 16:03

reef


People also ask

How can I compare two XML files online?

Copy and paste, drag and drop a XML file or directly type in the editors above, and then click on "Compare" button they will be compared if the two XML are valids. You can also click on "load XML from URL" button to load your XML data from a URL (Must be https).

What is XMLUnit?

XMLUnit provides you with the tools to verify the XML you emit is the one you want to create. It provides helpers to validate against an XML Schema, assert the values of XPath queries or compare XML documents against expected outcomes.


1 Answers

You can relax some of the constraints used by XMLUnit when comparing to trees by setting properties on the org.custommonkey.xmlunit.XMLUnit class.

In your case, you probably want:

XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

You may also find the setIgnoredAttributeOrder property helpful as well.

like image 112
Ophidian Avatar answered Oct 13 '22 00:10

Ophidian