2013-05-22 71 views
1

我正在使用xmlunit來比較兩個文本文件。控件的XML是:XmlUnit和子節點

<books> 
    <book> 
     <name>Angels &amp; Demons</name> 
     <isbn>9971-5-0210-0</isbn> 
     <author>Dan Brown</author> 
     <category></category> 
    </book> 
</books> 

我比較這與另一塊xml具有和元素交換。

<books> 
    <book> 
     <isbn>9971-5-0210-0</isbn> 
     <name>Angels &amp; Demons</name> 
     <author>Dan Brown</author> 
     <category></category> 
    </book> 
</books> 

的DIFF對象報告下列區別:

Expected sequence of child nodes '1' but was '3' - comparing <name...> at /books[1]/book[1]/name[1] to <name...> at /books[1]/book[1]/name[1] 

如果<name>是子節點',不會<isbn>是子節點 '2'?

+0

好像XMLUnit測試在XML子節點計數回車。設置以下內容\t \t ** XMLUnit.setIgnoreWhitespace(true); **給出了'期望的子節點序列'0'的更直觀結果,但是'1' - 比較了/ struct [1]/int [1]處的 at/struct [1]/int [1]' – timmy

回答

0

看起來像XmlUnit計數在xml中的回車作爲子節點。設置以下XMLUnit.setIgnoreWhitespace(true);給的子節點 '0' 的預期序列的更直觀的結果,但爲 '1' - 比較<int...> at /struct[1]/int[1] to <int...> at /struct[1]/int[1]

1

對於XMLUnit測試2.X版本:

對於XMLUnit測試2.xx的版本中,XMLUnit測試。 setIgnoreWhitespace(true)不再適用。現在,您可以通過添加DiffBuilder.ignoreWhitespace()將「忽略空白」直接添加到DiffBuilder中。

Diff diffXml =DiffBuilder.compare(expectedXml).withTest(actualXml).normalizeWhitespace().checkForSimilar().build(); 

斷言xml的類似,你可以例如。這樣做:

MatcherAssert.assertThat(diffXml.toString(), is("[identical]")); 

有關的1.x之間變化2.X furhter信息,請參閱: https://github.com/xmlunit/user-guide/wiki/Migrating-from-XMLUnit-1.x-to-2.x