使用 XMLUnit 2.X 比较 xml 文件时忽略特定节点的特定属性

2022-01-10 00:00:00 xml xml-parsing java xmlunit xmlunit-2

我有两个 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.

相关文章