Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two XML strings ignoring element order

Suppose I have two xml strings

<test>   <elem>a</elem>   <elem>b</elem> </test>  <test>   <elem>b</elem>   <elem>a</elem> </test> 

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)

   Diff diff = XMLUnit.compareXML(expectedString, actualString);       XMLAssert.assertXMLEqual("meh", diff, true); 
like image 550
Roay Spol Avatar asked May 14 '13 10:05

Roay Spol


1 Answers

For xmlunit 2.0 (I was looking for this) it is now done, by using DefaultNodeMatcher

Diff diff = Diffbuilder.compare(Input.fromFile(control))    .withTest(Input.fromFile(test))    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))    .build() 

Hope this helps this helps other people googling...

like image 100
Akzidenzgrotesk Avatar answered Sep 19 '22 14:09

Akzidenzgrotesk