-1

I wrote a short Java Tool for XML comparison using XMLunit. For my purposes it’s necessary to ignore the node order and some special element names. The attached code is working fine for that. Now I would like to get the differences. Unfortunately diff.toString() gives me all the differences also the “not identical” ones, but I would like to reduce the output to the real different ones because the “not identical” ones are caused by the dissentient order of the nodes.

My Output looks like:

org.custommonkey.xmlunit.Diff

[not identical] Expected sequence of child nodes '770' but was '771' …

[not identical] Expected sequence of child nodes '771' but was '772' …

[not identical] Expected sequence of child nodes '772' but was '773' …
…
[different] Expected text value 'Dominik' but was 'Dominika' - comparing <VORNAMEN ...>Dominik</VORNAMEN>…

My code is:

public void vergleichen()
{
    XMLUnit.setIgnoreAttributeOrder(Boolean.TRUE);
    XMLUnit.setIgnoreWhitespace(Boolean.TRUE);
    String f1 = lesen(quelle);
    String f2 = lesen(vergleich);

    try {
        Diff diff = new Diff(f1, f2);
        diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());

        diff.overrideDifferenceListener(new IgnoreNamedElementsDifferenceListener("ex1","ex2","ex3"));
        boolean result = diff.similar();

        if (result==true)
        {
            System.out.println("Die Dateien sind gleich");
        }
        else
        {
            System.out.println("Es wurden Unterschiede gefunden");
            System.out.println(diff.toString())
        }
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Thanks for help

Khalil M
  • 1,788
  • 2
  • 22
  • 36
Michael Konz
  • 170
  • 15

1 Answers1

0

Wrap your Diff inside a DetailedDiff before invoking similar and the DetailedDiff will allow you to access all Differences. You can then filter those Differences on whether they are recoverable and invoke toString on the non-recoverable ones yourself.

Stefan Bodewig
  • 3,260
  • 15
  • 22