XML JAVA 中的文件比较忽略节点顺序

XML file comparison in JAVA ignoring node order

我想使用 JAVA 比较 xml 个文件并检查它们是否 "equivalent"。 以下代码在两种情况下对我有用:

  1. 当 xml 个文件完全相同时。
  2. 当xml文件有差异时

但失败时:

  1. XML 个文件包含相同的节点,但它们的顺序不同。

一个名为 Sample.xml 的文件,其内容为:

<Employee>
    <FirstName>Jack</FirstName>
    <LastName>Dave</LastName>
    <Age>21</Age>
    <Professtion>Doctor</Professtion>
</Employee>

另一个名为 Sample1.xml 的文件,其内容为:

<Employee>
    <Age>21</Age>
    <Professtion>Doctor</Professtion>
    <FirstName>Jack</FirstName>
    <LastName>Dave</LastName>
</Employee>

注意内容相同,但顺序不同。

我尝试了[这个],但它对我不起作用。

我试过的代码如下:

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());
        }
    }

}

我也试过下面的代码:

Diff diff = DiffBuilder.compare(bufferedReaderExistingFile)
    .withTest(bufferedReaderNewFile).ignoreComments()
    .ignoreWhitespace()
    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
    .checkForSimilar()
    .build();

System.out.println("  difference are :: " + diff.hasDifferences());

您可以使用 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();


您可以使用 Diff 中的 similar() 方法,如 documentation:[=14= 中所述]

Return the result of a comparison. Two documents are considered to be "similar" if they contain the same elements and attributes regardless of order.

您的代码可能是这样的:

Diff diff = XMLUnit.compareXML(bufferedReaderExistingFile, bufferedReaderNewFile);
System.out.println("Both documents are similar: " + diff.similar());

请注意 XML matters 中的顺序,因此您提供的两个 XML 示例本质上是不同的 XML。