比较两个简单的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());
}
}
[]
答案 0 :(得分:1)
您的根元素flowers
与NodeFilter
不匹配,因此最终根本没有进行比较。
NodeFilter
最适合拒绝您不想比较的节点列表。在您的情况下,您需要确保您还允许遍历到您感兴趣的节点时遇到的所有节点。