简体   繁体   English

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

[英]Ignoring a particular attribute of a specific Node while comparing xml files using XMLUnit 2.X

I have two XML files:我有两个 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>

I have to ignore the attribute: id of Node: note while comparing these two files.在比较这两个文件时,我必须忽略属性:节点的idnote

I am using DiffBuilder :我正在使用DiffBuilder

Diff documentDiff = DiffBuilder.compare(srcFile).withTest(destFile).build()

Most online solutions suggested implementing DifferenceEvaluator :大多数在线解决方案建议实施DifferenceEvaluator

Tried that as well, but this ignores all nodes with attribute id, whereas I want to ignore an attribute from a specific node:也尝试过,但这会忽略具有属性 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;
        }
    }

Calling method in my Test class:在我的测试类中调用方法:

final Diff documentDiff = DiffBuilder.compare(src).withTest(dest)
.withDifferenceEvaluator(new IgnoreAttributeDifferenceEvaluator("id"))
.build();

Can someone suggest me a way I can achieve this using XMLUnit 2.x New to XMLUnit so please help accordingly.有人可以建议我使用 XMLUnit 2.x 来实现这一点 新的 XMLUnit,所以请相应地提供帮助。

Thanks in advance.提前致谢。

You could use a DifferenceEvaluator if you really wanted to.如果您真的愿意,您可以使用DifferenceEvaluator 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.除了属性本身的名称之外,您所要做的就是测试Attr的“所有者元素”的名称。

But XMLUnit 2.x offers a different solution to this: AttributeFilter .但是 XMLUnit 2.x 对此提供了不同的解决方案: AttributeFilter The code won't be that different from the DifferenceEvaluator you've already got, but you won't be mixing concerns.该代码DifferenceEvaluator您已经获得的DifferenceEvaluator没有太大区别,但您不会混淆关注点。

class IgnoreNoteId implements Predicate<Attr> {

    public boolean test(Attr attr) {
        return !("note".equals(attr.getOwnerElement().getNodeName())
            && "id".equals(attr.getNodeName()));
    }
}

or even shorter with a lambda in withAttributeFilter when using Java8.甚至在使用 Java8 时在withAttributeFilter使用 lambda 更短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM