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

大憨熊 提交于 2019-11-29 07:16:26

I found the solution by myself.

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

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();
fatih tekin

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>MVNO_Automation_8@test.fr</Value>
        </CustomerData>
        <CustomerData>
            <Key>DATE_OF_BRITH</Key>
            <Value>01-01-1976</Value>
        </CustomerData>
    </CustomerDataSet>

            <CustomerDataSet>
        <CustomerData name = "bla">
            <Key>EMAIL_ADDRESS</Key>
            <Value>MVNO_Automation_8@test.fr</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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!