【问题标题】:Comparing XML documents in Java diff class - getting line number?比较 Java diff 类中的 XML 文档 - 获取行号?
【发布时间】:2018-06-25 10:45:54
【问题描述】:

我有以下方法:

public static void assertXMLEquals(String expectedXML, String actualXML) throws Exception {
            XMLUnit.setIgnoreWhitespace(true);
            XMLUnit.setIgnoreAttributeOrder(true);

            DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML));

            List<Difference> allDifferences = diff.getAllDifferences();

            Assert.assertEquals("Differences found: " + diff.toString(), 0, allDifferences.size());
        }

来自班级:http://www.xmlunit.org/api/java/2.4.0/org/custommonkey/xmlunit/Diff.html

如果有差异,它会打印出来,例如:

[different] Expected attribute value 'false' but was 'true' - comparing <attribute name="false"...> at //journey[1]/attribute[1]/@name to <attribute name="true"...> at /journeyDetails[1]/journey[1]/attribute[1]/@name

有没有办法可以覆盖它来打印行号?

【问题讨论】:

  • 抱歉,我误读了您的帖子并指出了另一个 Javadoc,因为我认为您使用的是 2.x API 而不是旧版 API。

标签: java xml junit tostring xmlunit


【解决方案1】:

XMLUnit 的差异引擎在 DOM 节点级别工作,并且 DOM API 不公开此位置信息,因此它根本不能作为 Difference 的一部分使用。所以不,你根本无法掌握行号。

如果可以并且您使用的是 XMLUnit 2.x API 而不是旧版 API,则可以使用 DifftoString 重载,它采用 ComparisonFormatter 和您自己的实现.但这没有实际意义,因为 XMLUnit 1.x 和 2.x 都不能为您提供行号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2019-11-06
    相关资源
    最近更新 更多