gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:46:00 26 4
gpt4 key购买 nike

我想使用 JAVA 比较 xml 文件并检查它们是否“等效”。下面的代码在两种情况下适用于我:

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

但在以下情况下失败:

  • 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>

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

    我尝试过[这个1 ] 但它对我不起作用。

    我尝试的代码如下:

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


    关于java - JAVA中XML文件比较忽略节点顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993321/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com