Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

xml

xmlunit

I am trying successfully XmlUnit, and is very helpful in my job. Now, I have a little problem, that I don't know how to solve. I have a java class, that has a Set, and when transforming it into XML, the elements inside can have any order.

When I try these two files in XmlUnit it works (Diff says that they are similar):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Monitor>
    <AvailableMeasures>
        <MeasureDescriptorA name="netInput_mynetwork"></MeasureDescriptorA>
        <MeasureDescriptor name="netInput_myothernetwork"></MeasureDescriptor>
    </AvailableMeasures>
</Monitor>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Monitor>
    <AvailableMeasures>
        <MeasureDescriptor name="netInput_myothernetwork"></MeasureDescriptor>
        <MeasureDescriptorA name="netInput_mynetwork"></MeasureDescriptorA>
    </AvailableMeasures>
</Monitor>

But when the tags have the same name (with different attributes) it doesn't work (it mixes the attributes, and expect the one in the other tag):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Monitor>
    <AvailableMeasures>
        <MeasureDescriptor name="netInput_myothernetwork"></MeasureDescriptor>
        <MeasureDescriptor name="netInput_mynetwork"></MeasureDescriptor>
    </AvailableMeasures>
</Monitor>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Monitor>
    <AvailableMeasures>
        <MeasureDescriptor name="netInput_mynetwork"></MeasureDescriptor>
        <MeasureDescriptor name="netInput_myothernetwork"></MeasureDescriptor>
    </AvailableMeasures>
</Monitor>

Is there any workaround?

like image 985
greuze Avatar asked Jul 05 '11 09:07

greuze


1 Answers

I found the solution by myself.

Diff diff = new Diff(controlXml, responseXml);
diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
like image 177
greuze Avatar answered Sep 21 '22 01:09

greuze