javaxmlunit-2

XMLUNIT How do I ignore multiple elements when comparing XML?


I am able to compare 2 xml files using XMLUNIT 2.5 DiffBuilder. I want to ignore certain elements using withNodeFilter. But the withNodeFilter accepts only 1 element. Is there a way to ignore more that one element for comparison.

DiffBuilder.compare(DocB)
.withTest(docA)
.withNodeFilter(node -> !node.getNodeName().equals("metadata")) // need to include more element tags to ignore
.build();

Solution

  • You can just use logic operators, for example,

    .withNodeFilter(node -> !(node.getNodeName().equals("metadata") ||
                              node.getNodeName().equals("comment"))
    

    This will match all nodes which are not metadata or comment.