本文介绍了使用XMLUnit 2.X比较xml文件时,忽略特定节点的特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在比较这两个文件时,我必须忽略节点: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;
        }
    }

我的Test类中的调用方法:

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

10-27 00:55