2016-11-30 62 views
2

我想比較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

回答

0

嘗試使用此:

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

assert diff.hasDifferences()==false