I have couple of xmls which needs to be compared with different set of similar xml and while comparing i need to ignore tags based on a condition, for example
- personal.xml - ignore fullname
- address.xml - igone zipcode
- contact.xml - ignore homephone
here is the code
Diff documentDiff=DiffBuilder
.compare(actualxmlfile)
.withTest(expectedxmlfile)
.withNodeFilter(node -> !node.getNodeName().equals("FullName"))
.ignoreWhitespace()
.build();
How can i add conditions at " .withNodeFilter(node -> !node.getNodeName().equals("FullName")) " or is there a smarter way to do this
You can join multiple conditions together using "and" (
&&):If you want to keep your builder tidy, you can move the node filter to a separate method:
The risk with this is you may have element names which appear in more than one type of file - where that node needs to be filtered from one type of file, but not any others.
In this case, you would also need to take into account the type of file you are handling. For example, you can use the file names (if they follow a suitable naming convention) or use the root elements (assuming they are different) - such as
<Personal>,<Address>,<Contact>- or whatever they are, in your case.However, if you need to distinguish between XML file types, for this reason, you may be better off using that information to have separate
DiffBuilderobjects, with different filters. That may result in clearer code.