javaxmljunittostringxmlunit

Comparing XML documents in Java diff class - getting line number?


I have the following method:

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());
        }

It is from the class: http://www.xmlunit.org/api/java/2.4.0/org/custommonkey/xmlunit/Diff.html

If there are differences it prints out e.g :

[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

Is there a way I can override this to print the line number?


Solution

  • XMLUnit's difference engine works at the DOM Node level and the DOM API doesn't expose this location information, so it is not available as part of the Difference at all. So no, you can not get hold of the line number at all.

    If you could and you were using the XMLUnit 2.x API rather than the legacy one, the way to go was to use the toString overload of Diff that takes a ComparisonFormatter with an implementation of your own. But this is moot as neither XMLUnit 1.x nor 2.x can provide you with line numbers.