Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the solution by myself.

Diff diff = new Diff(controlXml, responseXml);
diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...