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

Ignoring a particular attribute of a specific Node while comparing xml files using XMLUnit 2.X(使用 XMLUnit 2.X 比较 xml 文件时忽略特定节点的特定属性)
本文介绍了使用 XMLUnit 2.X 比较 xml 文件时忽略特定节点的特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有两个 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 文件时忽略特定节点的特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)