gpt4 book ai didi

java - 将 XML 相等文件(空白除外)与 xmlunit 进行比较会产生差异

转载 作者:行者123 更新时间:2023-12-02 02:02:07 25 4
gpt4 key购买 nike

使用 xmlunit(版本 2.6.0)比较两个 XML 文件会产生差异,而实际上它们应该被视为相等。

XML 1:

<top><a>one</a><b>two</b></top>

XML 2:

<top>
<a>one</a>
<b>two</b>
</top>

Java代码: 源 xmlSource1 = Input.fromFile(xmlFile1).build(); 源 xmlSource2 = Input.fromFile(xmlFile2).build();

DefaultNodeMatcher nodeMatcher = new DefaultNodeMatcher(ElementSelectors.byNameAndText);

Diff d = DiffBuilder.compare(xmlSource1)
.withNodeMatcher(nodeMatcher)
.withTest(xmlSource2).build();
Iterable<Difference> diffList = d.getDifferences();
Iterator<Difference> iterator = diffList.iterator();
while(iterator.hasNext()) {
Difference next = iterator.next();
log.info("Difference: " + next);
}

产生此输出:

Difference: Expected child 'top' but was 'null' - comparing <top...> at /top[1] to <NULL> (DIFFERENT)
Difference: Expected child 'null' but was 'top' - comparing <NULL> to <top...> at /top[1] (DIFFERENT)

问题:为什么它们被认为是不同的?如何通过忽略空白差异来进行比较?理想情况下,我希望 d.hasDifferences() 为 false。

最佳答案

只需忽略空格(和注释)并执行检查差异中不包含相似之处(请参阅 checkForSimilar()):

Diff d = DiffBuilder.compare(xmlSource1).withTest(xmlSource2)
.checkForSimilar()
.withNodeMatcher(nodeMatcher)
.ignoreWhitespace()
.ignoreComments()
.build();

关于java - 将 XML 相等文件(空白除外)与 xmlunit 进行比较会产生差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51428038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com