Skip to content
Advertisement

Compare two XML strings ignoring element order

Suppose I have two xml strings

JavaScript

How to write a test that compares those two strings and ignores the element order?

I want the test to be as short as possible, no place for 10-line XML parsing etc. I’m looking for a simple assertion or something similar.

I have this (which doesn’t work)

JavaScript

Advertisement

Answer

My original answer is outdated. If I would have to build it again i would use xmlunit 2 and xmlunit-matchers. Please note that for xml unit a different order is always ‘similar’ not equals.

JavaScript

If somebody still want’t to use a pure java implementation here it is. This implementation extracts the content from xml and compares the list ignoring order.

JavaScript
Advertisement