2

我想比较 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)]

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

4

1 回答 1

0

尝试使用这个:

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

assert diff.hasDifferences()==false
于 2016-12-01T09:29:02.900 回答