我有两个 XML 文件:
I have two XML files:
<!------------------------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>
在比较这两个文件时,我必须忽略 Node:note 的属性:id.
I have to ignore the attribute:id of Node:note while comparing these two files.
我正在使用 DiffBuilder:
Diff documentDiff = DiffBuilder.compare(srcFile).withTest(destFile).build()
大多数在线解决方案建议实现 DifferenceEvaluator:
Most online solutions suggested implementing DifferenceEvaluator:
也试过了,但这会忽略所有具有属性 id 的节点,而我想忽略来自特定节点的属性:
Tried that as well, but this ignores all nodes with attribute id, whereas I want to ignore an attribute from a specific node:
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;
}
}
在我的测试类中调用方法:
Calling method in my Test class:
final Diff documentDiff = DiffBuilder.compare(src).withTest(dest)
.withDifferenceEvaluator(new IgnoreAttributeDifferenceEvaluator("id"))
.build();
有人可以建议我使用 XMLUnit 2.x 实现这一目标的方法吗XMLUnit 的新手,因此请提供相应的帮助.
Can someone suggest me a way I can achieve this using XMLUnit 2.x New to XMLUnit so please help accordingly.
提前致谢.
如果你真的想要,你可以使用 DifferenceEvaluator.除了属性本身的名称之外,您所要做的就是测试 Attr 的所有者元素"名称的名称.
You could use a DifferenceEvaluator if you really wanted to. All you'd have to do was testing the name of the Attr's "owner element"'s name in addition to the name of the attribute itself.
但 XMLUnit 2.x 对此提供了不同的解决方案:AttributeFilter.代码与您已经获得的 DifferenceEvaluator 不会有太大不同,但您不会混淆关注点.
But XMLUnit 2.x offers a different solution to this: AttributeFilter. The code won't be that different from the DifferenceEvaluator you've already got, but you won't be mixing concerns.
class IgnoreNoteId implements Predicate<Attr> {
public boolean test(Attr attr) {
return !("note".equals(attr.getOwnerElement().getNodeName())
&& "id".equals(attr.getNodeName()));
}
}
使用 Java8 时,在 withAttributeFilter 中使用 lambda 甚至更短.
or even shorter with a lambda in withAttributeFilter when using Java8.
这篇关于使用 XMLUnit 2.X 比较 xml 文件时忽略特定节点的特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
上传进度侦听器未触发(Google 驱动器 API)Upload progress listener not fired (Google drive API)(上传进度侦听器未触发(Google 驱动器 API))
使用 Google Drive SDK 将文件保存在特定文件夹中Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 将文件保存在特定文件夹中)
Google Drive Android API - 无效的 DriveId 和 Null ResourcGoogle Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 无效的 DriveId 和 Null ResourceId)
谷歌驱动api服务账户查看上传文件到谷歌驱动使Google drive api services account view uploaded files to google drive using java(谷歌驱动api服务账户查看上传文件到谷歌驱动使用java
Google Drive 服务帐号返回 403 usageLimitsGoogle Drive service account returns 403 usageLimits(Google Drive 服务帐号返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory;Google Drcom.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例