Search code examples
javaxmlxmlunit

Does XMLUNIT provide option to ignore certain elements in XML's for comparison?


I am wondering if XMLUNIT provides a way to ignore some of the elements present in the XML before doing the comparison.For example, if I want to ignore field which is randomly generated by the server.Is there anything available out of the box in XMLUnit to ignore certian elements or I need to write a custom DifferenceListener ?

Also, does it provide the elements name that do not match? If not, then what could be the best way to compare two XML which can allow me to ignore some elements and also provide me the elements names/values that does not match?


Solution

  • I'm afraid a custom DifferenceListener is the only way to go right now. There is a feature request for XMLUnit2 (https://github.com/xmlunit/xmlunit/issues/26) that hasn't been implemented, yet.

    Implementing the DifferenceListener may be a bit cumbersome since you'll not only receive Differences for the elements you want to ignore but most likely also receive them for the number of children of the parent element.

    Each Difference contains NodeDetails for the nodes seen on the test and control side and the NodeDetail contains the DOM Node (which will be an Element in your case).