乱序的元素构成可恢复的差异,因此Diff和DetailedDiff会说文档是相似的"但不是相同的".因此,要么忽略可恢复的差异,要么必须覆盖DifferenceListener而不是ElementQualifier来将类型CHILD_NODELIST_SEQUENCE_ID的差异从RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR(默认)降级为RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL.像public int differenceFound(Difference difference) { return difference.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL : RETURN_ACCEPT_DIFFERENCE;}接受默认设置,但仅降级乱序差异.I want to compare two xml files using XMLUnit. I would like the DetailedDiff to not report identical tags in different orders as differences. For example, if I created a DetailedDiff with these two snippets: <a><b/><c/></a>and<a><c/><b/></a>The DetailedDiff will create two Differences since the b and c tags are out of order. I have tried overriding element qualifiers but it doesn't result in any changes. Am I doing something wrong or is this impossible to do with XMLUnit? For reference here's the code I'm using to compare two xml files (not including overrideElementQualifier calls).public List<Difference> getDifferenceList(Reader file1, Reader file2) { Diff d = new Diff(file1, file2); //I'm passing the args as FileReaders d.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); detailedDiff = new DetailedDiff(d); List<Difference> allDifferences = detailedDiff.getAllDifferences(); return allDifferences;} 解决方案 RecursiveElementNameAndTextQualifier will yield the same result as the default ElementNameQualifier - b and c are out of order but other than that the documents are identical.Elements that are out of order constitute a recoverable difference, so Diff and DetailedDiff will say the documents are "similar" but not "identical". So either you ignore recoverable differences or you must override the DifferenceListener rather than the ElementQualifier to downgrade a difference of type CHILD_NODELIST_SEQUENCE_ID from RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR (the default) to RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL. Something likepublic int differenceFound(Difference difference) { return difference.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL : RETURN_ACCEPT_DIFFERENCE;}which accepts the default but downgrades just the out-of-order differences. 这篇关于如何使用XMLUnit的DetailsDifference忽略相同元素的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-23 13:56