Reputation: 41
Is there any way to completely ignore an empty tag in XMLUnit so that it is not taken into account for comparison? Ex:
<a>
<b/>
<c>data1</c>
<d>data2</d>
</a>
and
<a>
<c>data1</c>
<d>data2</d>
</a>
should not return any difference. Is there any inbuilt function for doing this? Thank you.
Upvotes: 4
Views: 1572
Reputation:
In general an empty tag is not the same as a missing tag, that's why XMLUnit complains. In XMLUnit 2.x you can suppress the comparison of nodes with a NodeFilter
. NodeFilter
is a predicate function that accepts a single DOM Node
and returns true
if the node should be considered while comparing documents. The default-implementation simply returns true
for all Node
s that are not the document type declaration.
You could use something like the following (untested) class
class SuppressEmptyElements implements Predicate<Node> {
@Override
public boolean test(Node n) {
if (n instanceof Element) {
return !isEmpty((Element) n);
}
// not an element - a commment, nested text and so on
return true;
}
private boolean isEmpty(Element e) {
return e.getAttributes().getLength() == 0 && e.getChildNodes().getLength() == 0;
}
}
When using DiffBuilder
you'd use something like withNodeFilter(new SuppressEmptyElements())
. The longer story can be found in XMLUnit's user guide.
Upvotes: 5