Compare unsorted (unordered) – whitespace different XML files

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
  • unordered
  • 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.

maven dependancies.


java class

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


Share this on your world...Share on Facebook
Tweet about this on Twitter
Share on LinkedIn
Email this to someone
Print this page

Leave a Reply