How can I ignore non-existing node element in XML Comparison Result?
<p>I am trying to compare <code>PUT Request XML</code> and <code>GET Response XML</code>. The following code works so good but according to the requirements some elements are only readable and therefore appears in GET Response XML file. How can I eliminate such a following result?</p>

<p>Result:</p>

<pre><code>[different] Expected presence of child node 'null' but was 'bs:key_id' - comparing  at null to &lt;bs:key_id...&gt; at /container[1]/int_user_object[1]/attributes[1]/key_id[1]
</code></pre>

<p>What I tried:</p>

<pre><code>import java.io.IOException;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.SAXException;
    public class XMLComparator {

        static Logger logger = LogManager.getLogger(XMLComparator.class);

        public boolean compare(String xml1, String xml2) throws SAXException, IOException {

            XMLUnit.setIgnoreWhitespace(true);
            XMLUnit.setIgnoreComments(true);
            XMLUnit.setIgnoreAttributeOrder(true);

            DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2)); //  wrap the Diff inside Detailed Diff.
            List&lt;?&gt; allDiff = diff.getAllDifferences();
            boolean result = diff.similar();
            if (result == false) {

                logger.info("There are " + allDiff.size() + " differences. XML difference(s): " + diff.toString());

            } else {
                logger.info("Sending XML and received XML are same: " + result);
            }
            return result;

        }

    }
</code></pre>

<p>I planned to add such a logic in my code after reading this document <a href="http://xmlunit.sourceforge.net/userguide/html/ar01s03.html" rel="nofollow noreferrer">doc</a> but I could not.</p>

<pre><code>if(NODE_TYPE_ID == null){
difference.ignore();} 
</code></pre>

<p>According to the Stefan help you can see the last version of my code</p>

<p>EDIT</p>

<pre><code>public class XMLComparator {

    static Logger logger = LogManager.getLogger(XMLComparator.class);

    public boolean compare(String xml1, String xml2) throws SAXException, IOException {

        XMLUnit.setIgnoreWhitespace(true);
        XMLUnit.setIgnoreComments(true);
        XMLUnit.setIgnoreAttributeOrder(true);

        DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2)); 
        diff.overrideDifferenceListener(new DifferenceListener() {
            @Override
            /* CHILD_NODE_NOT_FOUND_ID is used: A child node in one piece of XML could not be match against any other node of the other piece. 
             * 
             */
            public int differenceFound(Difference difference) {
                return difference.getId() == DifferenceConstants.CHILD_NODE_NOT_FOUND_ID 
                        ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL : RETURN_ACCEPT_DIFFERENCE;
            }


            @Override
            public void skippedComparison(Node arg0, Node arg1) {

            }
        });
        List&lt;?&gt; allDiff = diff.getAllDifferences();
        boolean result = diff.identical();
        if (result == false) {

            logger.info("There are " + allDiff.size() + " differences. XML difference(s): " + diff.toString());

        } else {
            logger.info("Sending XML and received XML are same: " + result);
        }
        return result;

    }

}
</code></pre>