Home>

I am creating a java program that uses an xml collation library called xmlunit.
Performs XML collation and returns a String (a list containing the number of mismatches) as shown below if there is a difference.

Expected text value'TEST' but was'TETS'
 comparing<TEST-TAG ...>TEST</TEST-TAG>
     at/TEST [1]/TEST-TAG [1]/text () [1]
  to<TEST-TAG ...>TETS</TEST-TAG>
     at/TEST [1]/TEST-TAG [1]/text () [1] (DIFFERENT)
* Line breaks have been added manually for visibility.

There is a problem with this (default) output format (*), so I want to specify the output format for output.
* For example, if you use "to" to indexOf to get the delimiter position and "to" is included in the character string to be compared, you will get the wrong delimiter position.

Official documentationI understand that you should probably set the option withComparisonFormatter.

There is this. (only this)

However, this implementation has not been successful.
(It can be applied if there is a concrete example somewhere ... but probably not in English ... I asked a question.)

At present, with the following code, we can confirm that there is no error (there is no problem in specifying the option).

Diff diff = DiffBuilder
            . ~ (Omitted)
            .withComparisonFormatter (null)
            .build ();
… In this case, withComparisonFormatter (null) is specified as null (→ default applied). I want to write the null part here properly.

In the image above

withComparisonFormatter (ComparisonFormatter formatter)


IsComparisonFormatterI understand that I should create a class with the interface, and I have tried various ways of writing it as follows, but the error (which is automatically issued on eclipse) has not been resolved. ..

Diff diff = DiffBuilder
            . ~ (Omitted)
            .withComparisonFormatter (~ implements ComparisonFormatter->{
                  try ~
                })
            .build ();


(* As a prerequisite understanding, if the specific description is incorrect,
I would like you to teach it. .. Assuming it matches)
At present, we do not know the correct one as to how to write the part of.

If there is any lack of information, please let us know. Thank you.

Addition

If it is as follows, I was able to go to the point where no error occurs. As of 2020/11/16 21:00, I don't know how to set the return value of another class, so I'm in the middle of trial and error.

Diff diff = DiffBuilder
            . ~ (Omitted)
            .withComparisonFormatter (new ShowDiffComparisonFormatter ())
            .build ();
public class ShowDiffComparisonFormatter implements ComparisonFormatter {
  public String getDescription (Comparison difference) {
    // TODO Auto-generated method stub
    return null;
  }
  public String getDetails (Detail details, ComparisonType type, boolean formatXml) {
    // TODO Auto-generated method stub
    return null;
  }
}
  • Answer # 1

    Note) IxmlunitI don't know anything about.

    I can apply it if there is a concrete example somewhere.

    See default implementation
    xmlunit/DefaultComparisonFormatter.java at main · xmlunit/xmlunit

    See other implementations
    Search · implements ComparisonFormatter