将两个XML文件与使用XMLUNIT 2.3.0顺序不顺序的子节点进行比较



我想比较soapui(groovy)中的两个XML文件,这些文件相似,但子节点不是顺序的。我正在使用XML单元v2.3.0。

XML1:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
</env:Header>
<soap:Body>
<Details>
<RateType RateTypeID="6">
    <RateType>AAAAA</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="3">
    <RateType>BBB</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="41">
    <RateType>CCC</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="43">
    <RateType>DDD</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
</Details>
</soap:Body>
</soap:Envelope>

xml2:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
</env:Header>
<soap:Body>
<Details>
<RateType RateTypeID="41">
    <RateType>CCC</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="43">
    <RateType>DDD</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="6">
    <RateType>AAAAA</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="3">
    <RateType>BBB</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
</Details>
</soap:Body>
</soap:Envelope>

在上面的示例中,XML的内容在内容上相似,但仅序列有所不同。我想比较他们两个,以了解它们是否相等。

当我运行以下代码时:

Diff myDiffSimilar = DiffBuilder.compare(XML1))
            .withTest(XML2)
            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("Rate").thenUse(ElementSelectors.selectorForElementNamed("RateValue", ElementSelectors.byNameAndAllAttributes)).build()))
            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("RateType").thenUse(ElementSelectors.selectorForElementNamed("RateType", ElementSelectors.byNameAndAllAttributes)).build()))
            .checkForSimilar().build();
log.info myDiffSimilar.getDifferences().toString();

它给我以下输出

[Expected child '{http://schemas.xmlsoap.org/soap/envelope/}Envelope' but was 'null' - comparing <soap:Envelope...> at /Envelope[1] to <NULL> (DIFFERENT), Expected child 'null' but was '{http://schemas.xmlsoap.org/soap/envelope/}Envelope' - comparing <NULL> to <soap:Envelope...> at /Envelope[1] (DIFFERENT)]

有人可以在这种情况下应使用的元素选择器/条件构建器吗?

尝试使用以下方式:

Diff diff = DiffBuilder.compare(actual)
            .withTest(expected)
            .ignoreComments()
            .ignoreWhitespace()
            .checkForSimilar()
            .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(), ElementSelectors.byName))
            .build();
assert diff.hasDifferences()==false

最新更新