如何比较 2 XML 个文件以及如何使用 XMLUnit 获取新的 Added\Removed 元素等信息

How to compare 2 XML files and how to fetch the informations like newly Added\Removed elements using XMLUnit

我有 2 个 xml 结构几乎相似的文件,在这些 XML 文件中如何获取以下信息 ??

  1. 新增控件(或节点)
  2. 删除了控件(或节点)并且
  3. 控件(或节点)属性已修改。

为了您的信息,我尝试了以下代码:

    File f1 = new File(inputXMLPath);
    File f2= new File(outputXMLPath);
    FileReader fr1 = null;
    FileReader fr2 = null;
    try {
        fr1 = new FileReader(f1);
        fr2 = new FileReader(f2);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {
        Diff diff = new Diff(fr1, fr2);
        System.out.println("Similar? " + diff.similar());
        System.out.println("Identical? " + diff.identical());

        DetailedDiff detDiff = new DetailedDiff(diff);

        detDiff.overrideDifferenceListener(new DifferenceListener() {
            @Override
            public int differenceFound(Difference diff) {
                if (diff.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
                    || diff.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID) {
                    return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
                }
                return RETURN_ACCEPT_DIFFERENCE;
            }
            @Override
            public void skippedComparison(Node arg0, Node arg1) { }

        });


        List differences = detDiff.getAllDifferences();
        for (Object object : differences) {
            Difference difference = (Difference)object;
            System.out.println("***********************");
            System.out.println(difference);
            System.out.println("***********************");
        }

    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

[编辑:添加了关于 setCompareUnmatched 和示例代码的注释]

我认为您想对 CHILD_NODE_NOT_FOUND Difference 作出反应,如果您使用 XMLUnit.setCompareUnmatched(false).

,XMLUnit 1.x 将发出

如果 Difference 的控制节点为空,则添加该节点 - 如果测试节点为空,则删除该节点。任何其他区别是对 XMLUnit 认为存在于任一侧的节点的更改。

例如

    FileReader fr1 = new FileReader(f1);
    FileReader fr2 = new FileReader(f2);
    XMLUnit.setCompareUnmatched(false);
    Diff diff = new Diff(fr1, fr2);
    System.out.println("Similar? " + diff.similar());
    System.out.println("Identical? " + diff.identical());

    DetailedDiff detDiff = new DetailedDiff(diff);

    detDiff.overrideDifferenceListener(new DifferenceListener() {
        @Override
        public int differenceFound(Difference diff) {
            if (diff.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
                || diff.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID) {
                return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
            return RETURN_ACCEPT_DIFFERENCE;
        }
        @Override
        public void skippedComparison(Node arg0, Node arg1) { }

    });


    List differences = detDiff.getAllDifferences();
    for (Object object : differences) {
        Difference difference = (Difference)object;
        System.out.println("***********************");
        if (difference.getId() == DifferenceConstants.CHILD_NODE_NOT_FOUND_ID) {
            if (difference.getControlNodeDetail().getNode() == null) {
                System.out.println("Node was added");
            } else {
                System.out.println("Node was removed");
            }
        }
        System.out.println("***********************");
    }

会导致类似

的结果
***********************
Expected presence of child node 'baz' but was 'null' - comparing <baz...> at /foo[1]/baz[1] to  at null
Node was removed
***********************
***********************
Expected presence of child node 'null' but was 'xyzzy' - comparing  at null to <xyzzy...> at /foo[1]/xyzzy[1]
Node was added
***********************