I have two xml files which I need to compare using xmlunit
content of these xml files are always same except their order
ex:
<report>
<component name = "a">
<component name = "b"/>
<component name = "c"/>
</component>
</report>
Here the order of the inside component may be different like below
<report>
<component name = "a">
<component name = "c"/>
<component name = "b"/>
</component>
</report>
When I compare these two xml I want diff.similar() to be true.
What I tried so far is
try(FileInputStream fileStream1 = new FileInputStream(expXMLPath)) {
try(FileInputStream fileStream2 = new FileInputStream(genXMLPath)) {
InputSource inputSource1 = new InputSource(fileStream1);
InputSource inputSource2 = new InputSource(fileStream2);
diff = new Diff(inputSource1, inputSource2);
RegDiffListener ignorableElementsListener = new RegDiffListener(
ignorableXPathsRegex);
diff.overrideDifferenceListener(ignorableElementsListener);
diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
diff.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(1, true));
diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
// diff.overrideElementQualifier(new
// ElementNameAndTextQualifier());
return diff;
}
I tried with different option of overrideElementQualifier and nothing is working. Please let me know the solution.
Using the solution mentioned by RC XMLUnit treat the documents as
similar
.If you want them to be treated as
identical
one solution could be to implement an DifferenceListener which ignores attribute differences at those nodes. Finde below a snippet for demonstration.output using
diff.overrideElementQualifier(new ElementNameAndAttributeQualifier())
output using
diff.overrideDifferenceListener(new IgnoreAttributeDifference())