2
    XML1:
    <record>
        <record name="technicalEnvelope" javaclass="XXX">
          <value name="flowReferenceId">xyz</value>
        </record>
        <record name="functionalEnvelope" javaclass="XXX">
          <value name="flowReferenceId">abc</value>
        </record>
    </record>
    XML2:
    <record>
        <record name="functionalEnvelope" javaclass="XXX">
          <value name="flowReferenceId">abc</value>
        </record>
        <record name="technicalEnvelope" javaclass="XXX">
          <value name="flowReferenceId">xyz</value>
        </record>
    </record>

    Diff myDiff = new Diff(XML1, XML2);
    assertTrue("pieces of XML are similar " + myDiff, myDiff.similar());

Comparison fails for these two similar XML due to different order. Any help/guidance would he highly appreciated.

Hoshang
  • 21
  • 2
  • I believe that xmlunit needs the sorted xml as input. – Rao Jan 27 '15 at 08:30
  • Thanks Rao for your comment but sorting will not help me here as one xml gets generated at runtime which is being compared with expected xml. – Hoshang Jan 27 '15 at 10:25
  • Had similar problem some time ago, so what i did was pass the runtime generated file and static file to a program which did the sorting, then pass that out put another utility like xmlunit for comparison. But that was for a specific type of xml, not for generic xml. – Rao Jan 27 '15 at 10:39
  • Thanks Rao ... I found a solution which I have posted below. – Hoshang Jan 27 '15 at 10:44

2 Answers2

0
    Diff myDiff = new Diff(myControlXML, myTestXML);
    myDiff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
    assertTrue("pieces of XML are similar " + myDiff, myDiff.similar());
Hoshang
  • 21
  • 2
0

For more complex elements you will need to tell XMLUnit how to identify the elements you want to compare with each other. This is exactly ElementQualifier's job. There are a bunch of built-in implementations of ElementQualifier (like ElementNameAndAttributeQualifier used by Hoshang) but if neither of them fits your requirements, you'll need to implement the interface yourself.

Stefan Bodewig
  • 3,260
  • 15
  • 22