比较两个XML文件与非按顺序使用xmlunit的子节点2.3.0

问题描述:

我想比较SoapUI(Groovy)中的两个XML文件,它们是相似的,但子节点不是按顺序排列的。我正在使用XML单元v2.3.0。比较两个XML文件与非按顺序使用xmlunit的子节点2.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)] 

可有人建议我在应该在这种情况下所使用的元素选择/有条件的建设者?

+0

欢迎堆栈溢出!对于任何想要重现输出结果的人来说,这肯定会有所帮助,前提是您可以将XML作为文本发布而不是图像。 – haindl

+1

我做到了!谢谢。 – KRS

+0

可能的重复 - http://stackoverflow.com/questions/40743664/groovy-compare-soap-response-with-xml-file – Rao

尝试使用此:

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

assert diff.hasDifferences()==false