我有以下 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;
}
生成的输出如下所示
/root[1]/a[1]
/root[1]/b[1]
/root[1]/c[1]
/root[1]/d[1]
感谢您的帮助
--SD