XMLUnit - compare xml and ignore few tags based on a condition

1.4k views Asked by At

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

  1. personal.xml - ignore fullname
  2. address.xml - igone zipcode
  3. 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

2

There are 2 answers

0
andrewJames On

You can join multiple conditions together using "and" (&&):

private static void doDemo1(File actual, File expected) {

    Diff docDiff = DiffBuilder
            .compare(actual)
            .withTest(expected)
            .withNodeFilter(
                    node -> !node.getNodeName().equals("FullName")
                    && !node.getNodeName().equals("ZipCode")
                    && !node.getNodeName().equals("HomePhone")
            )
            .ignoreWhitespace()
            .build();

    System.out.println(docDiff.toString());
}

If you want to keep your builder tidy, you can move the node filter to a separate method:

private static void doDemo2(File actual, File expected) {

    Diff docDiff = DiffBuilder
            .compare(actual)
            .withTest(expected)
            .withNodeFilter(node -> testNode(node))
            .ignoreWhitespace()
            .build();

    System.out.println(docDiff.toString());
}

private static boolean testNode(Node node) {
    return !node.getNodeName().equals("FullName")
            && !node.getNodeName().equals("ZipCode")
            && !node.getNodeName().equals("HomePhone");
}

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 DiffBuilder objects, with different filters. That may result in clearer code.

0
Shabrish Nayak On

I had provided the separate method in the below link for !node.getNodeName().equals("FullName")(which you are using in your code), I think by using that separate method you can just pass the array of nodes which you want to ignore and see the results. And incase you wish to add any other conditions based on your requirement, you can try and play in this method.

https://stackoverflow.com/a/68099435/13451711