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

3

6

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?

Araucaria answered 5/7, 2011 at 9:30 Comment(3)
I found the solution by myself, with this piece of code: Diff diff = new Diff(controlXml, responseXml); diff.overrideElementQualifier(new ElementNameAndAttributeQualifier()); it is done ;)Araucaria
can you please post an answer to the question yourself and then accept that answer? Also, you need to accept answers to previous questions if they fix your problem.Pleach
Ok, I thought I couldn't answer my own question. I will answer myself now.Araucaria
A
6

I found the solution by myself.

Diff diff = new Diff(controlXml, responseXml);
diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
Araucaria answered 27/6, 2012 at 7:43 Comment(0)
D
3

This seems to work for XMLUnit 2.0:

    Diff myDiff = DiffBuilder.compare(Input.fromString(expected))
            .withTest(Input.fromString(actual))
            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes))
            .checkForSimilar()
            .build();
Derose answered 9/2, 2016 at 11:31 Comment(0)
B
1

Above solution only works with attributes order but it will not work if you have issues with the same element type order like below:

    <CustomerDataSet>
        <CustomerData>
            <Key>ACCOUNT_TYPE</Key>
            <Value>GREEN</Value>
        </CustomerData>
        <CustomerData name = "bla">
            <Key>EMAIL_ADDRESS</Key>
            <Value>[email protected]</Value>
        </CustomerData>
        <CustomerData>
            <Key>DATE_OF_BRITH</Key>
            <Value>01-01-1976</Value>
        </CustomerData>
    </CustomerDataSet>

            <CustomerDataSet>
        <CustomerData name = "bla">
            <Key>EMAIL_ADDRESS</Key>
            <Value>[email protected]</Value>
        </CustomerData>
        <CustomerData>
            <Key>ACCOUNT_TYPE</Key>
            <Value>GREEN</Value>
        </CustomerData>
        <CustomerData>
            <Key>DATE_OF_BRITH</Key>
            <Value>01-01-1976</Value>
        </CustomerData>
    </CustomerDataSet>

However, you can work around this by using RecursiveElementNameAndTextQualifier instead.

Babbler answered 24/12, 2015 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.