使用 XMLUnit2 比较两个 xml 文件时如何打印所有差异

How to print all differences when comparing two xml files using XMLUnit2

使用 XMLUnit2 在 soapui 中使用 groovy 比较两个 xml 文件,它比较两个文件 successfully.Would 喜欢打印它发现的所有差异,但它只打印第一个差异. XMLUnit1 应该打印所有差异,但想使用 XMLUnit2。

如果哪位知道如何打印请帮帮我,不胜感激

代码使用:

diff = DiffBuilder.compare(resxml1)
        .withTest( resxml2)
        .withNodeFilter(nodeFilter)
        .withAttributeFilter(attributeFilter)
        .ignoreComments()
        .ignoreWhitespace()
        .ignoreElementContentWhitespace()
  .checkForSimilar()
        .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(), ElementSelectors.byName))
         .build();
print diff

您作为比较结果获得的 Diff 对象包含所有差异,您可以使用 diff.getDifferences() 访问它们。当您 print 时调用的 DifftoString 方法仅打印第一个差异。

所以如果你想打印所有差异你会做这样的事情

for (Difference d : diff.getDifferences()) {
    System.err.println(d);
}

为了更好地控制输出,请查看 ComparisonFomatterDifference 的单参数 toString 方法。