如何忽略 XML 比较结果中不存在的节点元素?

How can I ignore non-existing node element in XML Comparison Result?

我正在尝试比较 PUT Request XMLGET Response XML。下面的代码工作得很好,但根据要求,一些元素是只可读的,因此出现在 GET Response XML 文件中。我怎样才能消除这样的结果?

结果:

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

我尝试了什么:

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<?> 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;

        }

    }

我打算在阅读此文档后在我的代码中添加这样的逻辑doc,但我不能。

if(NODE_TYPE_ID == null){
difference.ignore();} 

根据Stefan的帮助你可以看到我代码的最新版本

编辑

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<?> 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;

    }

}

一种方法是实现 DifferenceListener 并抑制因缺少节点而产生的差异,例如

public int differenceFound(Difference difference) {
    return difference.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
        ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL
        : RETURN_ACCEPT_DIFFERENCE;
}

如果您可以选择切换到 XMLUnit 2.x,但是,您可以使用 NodeFilter 来完全隐藏您知道缺失的元素(而不是忽略所有缺失的元素) .