2

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

1 回答 1

1

[编辑:添加注释setCompareUnmatched和示例代码]

我认为您想对CHILD_NODE_NOT_FOUND DifferenceXMLUnit 1.x 如果您使用XMLUnit.setCompareUnmatched(false).

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

例如

    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
***********************
于 2015-08-07T13:25:03.493 回答