gpt4 book ai didi

recursion - 使用 XMLUnit 查找缺失元素

转载 作者:行者123 更新时间:2023-12-02 22:40:29 24 4
gpt4 key购买 nike


我有以下 testXML

<root>
<a>test</a>
<b>bee</b>
<d/>
</root>

虽然 templateXML 看起来像这样

<root>
<a/>
<b/>
<c/>
<d>
<e/>
</d>
</root>

我希望 XMLUnit 返回缺少的元素,在本例中缺少“c”和“e”

/root[1]/c[1] 丢失
/root[1]/d[1]/e[1] 丢失

我的代码如下所示

public static ArrayList<Difference> testCompareToSkeletonXML(String xml, String template) throws Exception {

XMLUnit.setCompareUnmatched(false);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
DetailedDiff myDiff = new DetailedDiff(new Diff(template, xml));
// myDiff.overrideDifferenceListener(myDifferenceListener);
myDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
ArrayList<Difference> returnList = new ArrayList<Difference>();
List<Object> allDifferences = myDiff.getAllDifferences();
for(Object obj: allDifferences){
Difference dif = (Difference) obj;
if(dif.getTestNodeDetail().getNode()== null){
//returnList.add(dif);
System.out.println(dif.getControlNodeDetail().getXpathLocation());
}
}
return returnList;
}

生成的输出如下所示

/根[1]/a[1]
/根[1]/b[1]
/根[1]/c[1]
/root[1]/d[1]

感谢您的帮助
--标清

最佳答案

您告诉 XMLUnit 仅当元素名称和嵌套文本相同时才相互匹配元素。 ab 元素的情况并非如此(它们的嵌套文本有所不同),这就是您在列表中看到它们的原因。

您会看到 d 而不是嵌套的 e,因为根据 RecursiveElementNameAndTextQualifier< 的规则,顶级 d 是不同的。/。如果删除设置 RecursiveElementNameAndTextQualifier 的行,您的列表将减少为

/root[1]/c[1]
/root[1]/d[1]/e[1]

这正是您所期望的。

关于recursion - 使用 XMLUnit 查找缺失元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com