我有两个 XML 文件:
<!------------------------File1--------------------------------->
<note id="ignoreThisAttribute_1">
<to>Experts</to>
<from>Matrix</from>
<heading id="dontIgnoreThisAttribute_1">Reminder</heading>
<body>Help me with this problem</body>
</note>
<!------------------------File2--------------------------------->
<note id="ignoreThisAttribute_2">
<to>Experts</to>
<from>Matrix</from>
<heading id="dontIgnoreThisAttribute_2">Reminder</heading>
<body>Help me with this problem</body>
</note>
在比较这两个文件时,我必须忽略id
Node:的属性。note
我正在使用DiffBuilder
:
Diff documentDiff = DiffBuilder.compare(srcFile).withTest(destFile).build()
大多数在线解决方案建议实施DifferenceEvaluator
:
也试过了,但这会忽略所有具有属性 id 的节点,而我想忽略来自特定节点的属性:
public class IgnoreAttributeDifferenceEvaluator implements DifferenceEvaluator {
private String attributeName;
public IgnoreAttributeDifferenceEvaluator(String attributeName) {
this.attributeName = attributeName;
}
@Override
public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) {
if (outcome == ComparisonResult.EQUAL)
return outcome;
final Node controlNode = comparison.getControlDetails().getTarget();
System.out.println(controlNode.getNodeName());
if (controlNode instanceof Attr) {
Attr attr = (Attr) controlNode;
if (attr.getName().equals(attributeName)) {
return ComparisonResult.EQUAL;
}
}
return outcome;
}
}
在我的测试类中调用方法:
final Diff documentDiff = DiffBuilder.compare(src).withTest(dest)
.withDifferenceEvaluator(new IgnoreAttributeDifferenceEvaluator("id"))
.build();
有人可以建议我使用 XMLUnit 2.x New to XMLUnit 实现此目的的方法,因此请提供相应的帮助。
提前致谢。