Note: if you can’t see below java sample code, that may be because you are behind a firewall / proxy and that caused restricted to access gist website
In day to day programming work, specially in unit test there are requirements to compare xml files. biggest problem with xml files they are meant to be valid even with
- whitespace different
- comment differences
- CData differences
Most of the time developers are trying to convert xml into String and do the comparison. if this is for unit testing performance may not be an issue. but again if format different or whitespace / comments are different this test will mark as fail and that is a false failure.
XMLUnit is the solution for this. below example will demonstrate how you can compare unsorted xml file even with whitespaces.
<dependencies> <dependency> <groupId>xmlunit</groupId> <artifactId>xmlunit</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
expected result xml file
current result xml file (unsorted)
when you execute this you can see no failures. but if you made any changes (content) to either xml file you will see fail on assert.
this Sample project can be download from https://github.com/krishantha/code-samples/tree/master/xmlDiff