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

柔情痞子 提交于 2019-12-04 04:55:12

问题


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>

I have to ignore the attribute:id of Node:note while comparing these two files.

I am using DiffBuilder:

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

Most online solutions suggested implementing DifferenceEvaluator:

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();

Can someone suggest me a way I can achieve this using XMLUnit 2.x New to XMLUnit so please help accordingly.

Thanks in advance.


回答1:


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.

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()));
    }
}

or even shorter with a lambda in withAttributeFilter when using Java8.



来源:https://stackoverflow.com/questions/45318688/ignoring-a-particular-attribute-of-a-specific-node-while-comparing-xml-files-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!