2

我正在尝试使用 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?如果是这样,还有另一种方法来区分标签吗?

4

1 回答 1

1

IgnoreTextAndAttributeValuesDifferenceListener downgrades the differences to "similar" not "identical", that's why they still show up in the list of Differences.

If you cannot live with "not identical" you'll need to use a DifferenceListener of your own - and will likely end up copying IgnoreTextAndAttributeValuesDifferenceListener changing just a single line.

于 2015-02-03T19:23:08.633 回答