15

I need to diff large directory structures containing generated .xml and .wsdl files. The generated .xml files differ in the order that child elements appear. The XmlUnit Diff.similar() method is supposed to handle this case:

'Two documents are considered to be "similar" if they contain the same elements and attributes regardless of order.'

This is not the case, however. I took a .xml file, reversed the order of two child elements and XmlUnit says that they are not similar.

XmlUnit is broken, providing no more functionality than the built-in diff utility.

Is there an alternative to XmlUnit that recognizes simple differences in .xml files like ordering of child elements?

Dean Schulze
  • 9,633
  • 24
  • 100
  • 165
  • 6
    If you want help you're going to have to edit your post to include (a) your code showing how you instantiated `Diff` and invoked `Diff.similar()`, and (b) a minimal example of XML that demonstrates what you perceive as the incorrect output. – Jim Garrison Jan 18 '11 at 20:23

5 Answers5

6

I have a similar problem, in my case, I had several tags with the same name, but different attributes (the order didn't matter), but XmlUnit always checked first with first, second with second... that could be swapped. My question was:

Comparing similar xml files with XmlUnit with unordered tags (same tag name with different attributes)

I found a solution here:

http://www.infoq.com/articles/xml-unit-test

In my case, it was solved just overriding the element qualifier:

    Diff diff = new Diff(controlXml, responseXml);
    diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
Community
  • 1
  • 1
greuze
  • 4,250
  • 5
  • 43
  • 62
3

I found out that setting the following option resolved the problem of inaccurate (un)similarity:

XMLUnit.setIgnoreWhitespace(true);
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
unludo
  • 4,912
  • 7
  • 47
  • 71
  • 3
    This is one of the main issues I have with XMLUnit, static setup causing thests to influence eachother. I'd rather see these calls on the Diff object. – Somatik Aug 22 '11 at 13:29
1

I agree with Jim's comment. Most of issues I faced with XmlUnit was due to incorrect initialization.

However, you could always take a look at the XMLUnit source code and substitute Diff handler with your own if you feel like implementation is not doing what you need. The code is quite easy to understand and you will be able to fully control behavior without much effort needed to implement your own diff engine.

oiavorskyi
  • 2,893
  • 1
  • 20
  • 23
1

It is better to sort XML, before comparing it with xmlunit, instead of using xmlunit sorting write your own, it would be much easier than configuring xmlunit for your needs

Darren
  • 68,902
  • 24
  • 138
  • 144
anonymous
  • 11
  • 1
0

have you tried XUnit?

Baget
  • 3,318
  • 1
  • 24
  • 44