問題描述
我有兩個 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()
大多數(shù)在線解決方案建議實現(xiàn) DifferenceEvaluator
:
Most online solutions suggested implementing DifferenceEvaluator
:
也試過了,但這會忽略所有具有屬性 id 的節(jié)點,而我想忽略來自特定節(jié)點的屬性:
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;
}
}
在我的測試類中調(diào)用方法:
Calling method in my Test class:
final Diff documentDiff = DiffBuilder.compare(src).withTest(dest)
.withDifferenceEvaluator(new IgnoreAttributeDifferenceEvaluator("id"))
.build();
有人可以建議我使用 XMLUnit 2.x 實現(xiàn)這一目標的方法嗎XMLUnit 的新手,因此請?zhí)峁┫鄳?yīng)的幫助.
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
.代碼與您已經(jīng)獲得的 DifferenceEvaluator
不會有太大不同,但您不會混淆關(guān)注點.
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.
這篇關(guān)于使用 XMLUnit 2.X 比較 xml 文件時忽略特定節(jié)點的特定屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!