【问题标题】:How can I ignore non-existing node element in XML Comparison Result?如何忽略 XML 比较结果中不存在的节点元素?
【发布时间】:2016-12-20 14:12:54
【问题描述】:

我正在尝试比较 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;

    }

}

【问题讨论】:

    标签: xml xmlunit


    【解决方案1】:

    一种方法是实现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 来完全隐藏您知道丢失的元素(而不是忽略所有丢失的元素)。

    【讨论】:

    • 比你好,但我仍然看到了所有结果,包括 [不同] 预期存在子节点“null”,但为“bs:key_id” - 在 null 与 进行比较/container[1]/int_user_object[1]/attributes[1]/key_id[1]
    • 我使用了 CHILD_NODE_NOT_FOUND_ID 而不是 CHILD_NODELIST_SEQUENCE_ID。因为我的问题不在于订单。我设法消除了其中一些,但我仍然无法满足我的结果。
    • 一个问题:我必须使用 NAMESPACE_PREFIX_ID 它是文档级别的常量。另一方面,CHILD_NODE_NOT_FOUND_ID 是元素级别。我可以有机会使用它们的机器人吗?如果是怎么办?我以编程方式尝试了 && 但它不起作用。
    • 我通过更改我的 xml 详细信息而不是使用 NAMESPACE_PREFIX_ID 解决了这个问题
    猜你喜欢
    • 2023-03-21
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多