Imp*_*ive 8 xml diff xmlunit xmlunit-2
使用XMLUnit 2,如何在不考虑元素顺序的情况下比较两个文档?
我为XMLUnit 1得到了这个问题,但显然v2中的新API不再具有上述方法了.
这是我目前的代码:
Diff diff = DiffBuilder.compare(expected)
.withTest(actual)
.ignoreComments()
.ignoreWhitespace()
.checkForSimilar()
.build();
assertFalse(diff.hasDifferences());
Run Code Online (Sandbox Code Playgroud)
编辑Stefan Bodewigs评论:
这些是我与上面的片段比较的两个字符串:
String expected = "<root><foo>FOO</foo><bar>BAR</bar></root>";
String actual = "<root><bar>BAR</bar><foo>FOO</foo></root>";
Run Code Online (Sandbox Code Playgroud)
报道的差异
Expected element tag name 'foo' but was 'bar' - comparing <foo...> at /root[1]/foo[1] to <bar...> at /root[1]/bar[1] (DIFFERENT)
Expected text value 'FOO' but was 'BAR' - comparing <foo ...>FOO</foo> at /root[1]/foo[1]/text()[1] to <bar ...>BAR</bar> at /root[1]/bar[1]/text()[1] (DIFFERENT)
Expected element tag name 'bar' but was 'foo' - comparing <bar...> at /root[1]/bar[1] to <foo...> at /root[1]/foo[1] (DIFFERENT)
Expected text value 'BAR' but was 'FOO' - comparing <bar ...>BAR</bar> at /root[1]/bar[1]/text()[1] to <foo ...>FOO</foo> at /root[1]/foo[1]/text()[1] (DIFFERENT)
Run Code Online (Sandbox Code Playgroud)
Ste*_*wig 12
在2.x文档中可能需要变得更清楚的一个区别是默认ElementSelector- 大致是ElementQualifier1.x中的.其中1.x默认按名称匹配元素,2.x默认按顺序匹配元素.也许这是一个坏主意.
如果切换到元素名称匹配,则Diff应该有效.
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))