0

I have 2 XMLs to be compared:

File1.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<member>
    <SchoolID>1021</SchoolID>
    <CandidateType>First Year</CandidateType>
    <CandidateName>John</CandidateName>
</member>

File2.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<member>
    <CandidateID>3147</CandidateID>
    <SchoolID>1021</SchoolID>
    <CandidateType>Second Year</CandidateType>
    <CandidateName>Peter</CandidateName>
</member>

I am using xmlunit to compare, however the output I am getting is like:

Similar? false
Identical? false
***********************
Expected number of child nodes '2' but was '3' - comparing <member...> at /member[1] to <member...> at /member[1]
***********************
***********************
Expected sequence of child nodes '0' but was '1' - comparing <SchoolID...> at /member[1]/SchoolID[1] to <SchoolID...> at /member[1]/SchoolID[1]
***********************
***********************
Expected text value 'John' but was 'Peter' - comparing <CandidateName ...>John</CandidateName> at /member[1]/CandidateName[1]/text()[1] to <CandidateName ...>Peter</CandidateName> at /member[1]/CandidateName[1]/text()[1]
***********************
***********************
Expected sequence of child nodes '1' but was '2' - comparing <CandidateName...> at /member[1]/CandidateName[1] to <CandidateName...> at /member[1]/CandidateName[1]
***********************
***********************
Expected presence of child node 'null' but was 'CandidateID' - comparing  at null to <CandidateID...> at /member[1]/CandidateID[1]

I need to represent the output such that it only tells me the following differences: Node CandidateID is missing in File1.xml and the data difference for Node CandidateName. I don't need the extra details. Is there a way to tweak the output of detDiff.getAllDifferences().

The code snapshot looks like:

try {
// fr1 and fr2 are my two xml files.
    Diff diff = new Diff(fr1, fr2); 
    System.out.println("Similar? " + diff.similar());
    System.out.println("Identical? " + diff.identical());

    DetailedDiff detDiff = new DetailedDiff(diff);
    List differences = detDiff.getAllDifferences();
    for (Object object : differences) {
        Difference difference = (Difference)object;
        System.out.println("***********************");
        System.out.println(difference);
        System.out.println("***********************");
    }
user2967948
  • 529
  • 2
  • 8
  • 23

1 Answers1

2

Depending on your needs you could either filter the differences after the comparison or override the DifferenceListener in order to ignore the differences you are not interested in.

For filtering it seems you could just look at isRecoverable and strip away all recoverable differences.

A custom DifferenceListener would be something like

    detDiff.overrideDifferenceListener(new DifferenceListener() {
            @Override
            public int differenceFound(Difference diff) {
                if (diff.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
                    || diff.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID) {
                    return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
                }
                return RETURN_ACCEPT_DIFFERENCE;
            }
            @Override
            public void skippedComparison(Node arg0, Node arg1) { }
        });

before you call getAllDifferences.

Stefan Bodewig
  • 3,260
  • 15
  • 22