Diff只是带有XMLUnit的XML标签(没有文本或属性)

时间:2014-07-09 00:24:40

标签: java xml diff xmlunit

我试图使用XMLUnit来区分XMLUnit的两个XML文档的整体结构(没有文本/属性),如下所示:

private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

        xhtml1 = XML_HEADER + "<b>Text</b>";
        xhtml2 = XML_HEADER + "<b>Different Text!</b>";

        Diff d = new Diff(xhtml1.trim(), xhtml2.trim());

        DetailedDiff dd = new DetailedDiff(d);
        dd.overrideElementQualifier(null);
        dd.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
        List<Difference> l = dd.getAllDifferences();
        for (Difference difference : l) {
            System.out.println(d.toString());
        }

尽管我覆盖了DetailedDiff上的差异监听器,但我仍然得到以下结果:

org.custommonkey.xmlunit.Diff
[not identical] Expected text value 'Text' but was 'Different Text!' - comparing <b ...>Text</b> at /b[1]/text()[1] to <b ...>Different Text!</b> at /b[1]/text()[1]

DifferenceListener不适用于getAllDifferences吗?如果是这样,还有另一种方法来区分标签吗?

1 个答案:

答案 0 :(得分:1)

IgnoreTextAndAttributeValuesDifferenceListener将差异降级为“相似”而不是“相同”,这就是为什么它们仍会显示在差异列表中。

如果您不能使用“不相同”,则需要使用自己的DifferenceListener - 并且可能最终会复制IgnoreTextAndAttributeValuesDifferenceListener只更改一行。