XMLUnit NodeFilter找不到区别

时间:2019-03-21 01:10:01

标签: xmlunit-2

比较两个简单的XML,并想比较具有特定localName的节点。在这种情况下,仅flowerA。当我使用不等于flowerB时,它会给我flowerA节点的差异,当我说等于flowerA时,它不会给任何差异?

    public class XMLDiff {
        public static void main(String[] args) {
            String controlXml = "<flowers><flowerA>Rose</flowerA><flowerB>Daisy</flowerB></flowers>";
            String testXml =    "<flowers><flowerA>Roses</flowerA><flowerB>Daisies</flowerB></flowers>";

            Diff build = DiffBuilder.compare(controlXml).withTest(testXml)
                    .ignoreWhitespace()
                    .withNodeFilter(node -> !node.getNodeName().equals("flowerB"))
                    .build();
            System.out.println(build.getDifferences());
        }
    }
[Expected text value 'Rose' but was 'Roses' - comparing <flowerA ...>Rose</flowerA> at /flowers[1]/flowerA[1]/text()[1] to <flowerA ...>Roses</flowerA> at /flowers[1]/flowerA[1]/text()[1] (DIFFERENT)]
    public class XMLDiff {
        public static void main(String[] args) {
            String controlXml = "<flowers><flowerA>Rose</flowerA><flowerB>Daisy</flowerB></flowers>";
            String testXml =    "<flowers><flowerA>Roses</flowerA><flowerB>Daisies</flowerB></flowers>";

            Diff build = DiffBuilder.compare(controlXml).withTest(testXml)
                    .ignoreWhitespace()
                    .withNodeFilter(node -> node.getNodeName().equals("flowerA"))
                    .build();
            System.out.println(build.getDifferences());
        }
    }
[]

1 个答案:

答案 0 :(得分:1)

您的根元素flowersNodeFilter不匹配,因此最终根本没有进行比较。

NodeFilter最适合拒绝您不想比较的节点列表。在您的情况下,您需要确保您还允许遍历到您感兴趣的节点时遇到的所有节点。