0

我正在尝试比较以下两个 XML... 我正在做相似性测试。

XML1

<top> <first>true</first> <!--- control node 1 ----> <second> <!--- control node 2 ----> <secondpart1>ooo</secondpart1> </second> <third> <!--- control node 3 ----> <thirdpart1>zzzz</thirdpar1> </third> <third> <!--- control node 4 ----> <thirdpart1>zzzz</thirdpar1> <thirdpart2>zzzz</thirdpar2> </third> </top>

XML2

<top> <second> <!--- test node 1 ----> <secondpart1>ooo</secondpart1> </second> <third> <!--- test node 2 ----> <thirdpart1>zzzz</thirdpar1> </third> <third> <!--- test node 3 ----> <thirdpart1>zzzz</thirdpar1> <thirdpart2>zzzz</thirdpar2> </third> <first>true</first> <!--- test node 4 ----> </top>

我收到这个错误

[不同] 子节点的预期数量为“1”,但为“2”。看起来它将控制节点 3 与测试节点 3 进行比较。由于元素的名称与“第三”匹配,因此它进行了比较。有解决这种情况的方法吗?

这是我的代码

    Diff d = new Diff(xmlResponseTrim, serializedResponseTrim);
    DetailedDiff dd = new DetailedDiff(d);
    dd.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
    System.out.println(dd);
    assertTrue(dd.similar());
4

1 回答 1

0

接口决定了 XMLUnit将ElementQualifier哪些元素相互比较。对于您的特定情况RecursiveElementNameAndTextQualifier,它会起作用,但这不是一个通用的解决方案。在更复杂的情况下,您必须在自己的实现中实现匹配策略。

于 2015-02-03T18:55:17.673 回答