Search code examples
javaxmlxmlunitxmlunit-2

XMLUnit-2 ignore certain nested XML elements


My XML is little complex and I've to ignore certain entry from the comparison, how would I able to achieve it ?

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE ResourceObject PUBLIC "my_corp.dtd" "my_corp.dtd">
<ResourceObject displayName="TESTNGAD\AggUserFSP test" identity="CN=AggUserFSP test,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local" objectType="account" uuid="{97182a65-61f2-443c-b0fa-477d0821d8c4}">
   <Attributes>
     <Map>
       <entry key="accountFlags">
         <value>
           <List>
             <String>Normal User Account</String>
             <String>Password Cannot Expire</String>
           </List>
         </value>
       </entry>
       <entry key="homePhone" value="6555"/>
       <entry key="l" value="Pune"/>
       <entry key="memberOf">
         <value>
           <List>
             <String>CN=FSPGRP2,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
             <String>CN=FSPGRP1,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
             <String>CN=LocalAggFrame,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local</String>
           </List>
         </value>
       </entry>
       <entry key="objectClass">
         <value>
           <List>
             <String>top</String>
             <String>person</String>
             <String>organizationalPerson</String>
             <String>user</String>
           </List>
         </value>
       </entry>
       <entry key="sn" value="test"/>
       <entry key="st" value="MH"/>
       <entry key="streetAddress" value="SB ROAD"/>
       <entry key="title" value="QA"/>
       <entry key="userPrincipalName" value="AggUserFSP test@TestNGAD.local"/>
     </Map>
   </Attributes>
 </ResourceObject>

I tried

Diff diff = DiffBuilder
             .compare(control)
             .withTest(test)
             .checkForSimilar().checkForIdentical()
             .normalizeWhitespace()
             .ignoreComments()
             .ignoreWhitespace()
             .withNodeFilter(node -> !(node.getNodeName().equals("accountFlags") ||
                            node.getNodeName().equals("homePhone"))).build();

But, it is not working. How should I ignore some XML entry here?


Solution

  • Neither "accountFlags" nor "homePhone" are element names, so my filter will not match anything.

    The NodeFilter must return true unless all of the following conditions are met

    • node is actually an Element
    • node has an attribute named "key"
    • the value of this attribute is either "accountFralgs" or "homePhone"
        private boolean filter(final Node n) {
            if (n instanceof Element) {
                final String attrValue = ((Element) n).getAttibute("key");
                // attrValue is th eempty string if the attribute is missing
                return !("accountFlags".equals(attrValue) || "homePhone".equals(attrValue));
            }
            return true;
        }