Reputation: 8658
I wanted to compare the xml
files using JAVA
and check if they are "equivalent".
The below code works for me in 2 cases:
But fails when:
One file called Sample.xml
having the content as:
<Employee>
<FirstName>Jack</FirstName>
<LastName>Dave</LastName>
<Age>21</Age>
<Professtion>Doctor</Professtion>
</Employee>
Another file called Sample1.xml
having the content as:
<Employee>
<Age>21</Age>
<Professtion>Doctor</Professtion>
<FirstName>Jack</FirstName>
<LastName>Dave</LastName>
</Employee>
Note the content is the same but the order is not.
I tried [this1] but it didn't work for me.
The code I tried follows:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
public class CompareXML {
public static void main(String[] args) {
try (BufferedReader bufferedReaderExistingFile = new BufferedReader(new FileReader("C:\\Test\\Sample.xml"));
BufferedReader bufferedReaderNewFile = new BufferedReader(new FileReader("C:\\Test\\Sample1.xml"))) {
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setIgnoreComments(true);
Diff d = new Diff(bufferedReaderExistingFile, bufferedReaderNewFile);
d.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
DetailedDiff detailedDiff = new DetailedDiff(d);
List<?> allDifferences1 = detailedDiff.getAllDifferences();
System.out.println(" difference are :: " + allDifferences1.isEmpty());
System.out.println(" difference are :: " + allDifferences1.size());
System.out.println(" difference are :: " + allDifferences1.toString());
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
I have also tried the below code :
Diff diff = DiffBuilder.compare(bufferedReaderExistingFile)
.withTest(bufferedReaderNewFile).ignoreComments()
.ignoreWhitespace()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
.checkForSimilar()
.build();
System.out.println(" difference are :: " + diff.hasDifferences());
Upvotes: 2
Views: 1903
Reputation: 3651
You can use the similar() method from Diff, as said in the documentation:
Return the result of a comparison. Two documents are considered to be "similar" if they contain the same elements and attributes regardless of order.
Your code could be something like this:
Diff diff = XMLUnit.compareXML(bufferedReaderExistingFile, bufferedReaderNewFile);
System.out.println("Both documents are similar: " + diff.similar());
Note though that order in XML matters, so the two XML samples you presented are essentially different XML.
Upvotes: 1
Reputation: 8163
You can do this with DifferenceEvaluators:
DifferenceEvaluator evaluator = DifferenceEvaluators
.downgradeDifferencesToEquals(ComparisonType.CHILD_NODELIST_SEQUENCE);
Diff diff = DiffBuilder.compare(bufferedReaderExistingFile)
.withTest(bufferedReaderNewFile).ignoreComments()
.ignoreWhitespace()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
.withDifferenceEvaluator(evaluator)
.checkForSimilar()
.build();
Upvotes: 4