开发者

Ignore few children for comparison when comparing xml documents with XMLUnit

开发者 https://www.devze.com 2023-01-16 04:48 出处:网络
Is it possible to ignore a few children in elements when comparin开发者_如何学编程g XML documents with XMLUnit? I want to ignore any empty text nodes in the element when comparing them.

Is it possible to ignore a few children in elements when comparin开发者_如何学编程g XML documents with XMLUnit? I want to ignore any empty text nodes in the element when comparing them.

Best Regards, Keshav


You can use this listener:

import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.DifferenceConstants;
import org.custommonkey.xmlunit.DifferenceListener;
import org.w3c.dom.Node;

import java.util.HashSet;
import java.util.Set;

public class IgnoreChildrenOfNamedElementsDifferenceListener implements DifferenceListener {
    public IgnoreChildrenOfNamedElementsDifferenceListener() {}

    public int differenceFound(Difference difference) {
        if (difference.getId() == DifferenceConstants.HAS_CHILD_NODES_ID ||
                difference.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID ||
                difference.getId() == DifferenceConstants.CHILD_NODE_NOT_FOUND_ID) {
                return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
        }    
        return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
    }

    @Override
    public void skippedComparison(Node control, Node test) {}
}

Then you can use it in following way:

Diff diff = new Diff(expectedDoc, obtainedDoc);
diff.overrideDifferenceListener(new IgnoreChildrenOfNamedElementsDifferenceListener("TestDateTime"));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号