XMLUnit - 忽略“id”比较属性

发布于 2024-10-21 05:19:17 字数 104 浏览 1 评论 0原文

我目前正在使用 XMLUnit,我想知道是否有方法将其配置为忽略我想要比较的标签的 id 属性。

预先感谢您的帮助。

I am currently working with XMLUnit and I am wondering if there is way to configure it to ignore only the id attribute of the tags I want to compare.

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

上课铃就是安魂曲 2024-10-28 05:19:17

解决方案非常简单。您可以配置 DifferenceEngine 来处理 ATTR_VALUE 差异。编写实现DifferenceListener的自定义差异监听器类:

class IgnoreIDsDifferenceListener implements DifferenceListener {
    private static final int[] IGNORE_VALUES = new int[] {
            DifferenceConstants.ATTR_VALUE.getId(),
    };

    private boolean isIgnoredDifference(Difference difference) {
        int differenceId = difference.getId();
        for (int i=0; i < IGNORE_VALUES.length; ++i) {
            if (differenceId == IGNORE_VALUES[i]) {
                return true;
            }
        }
        return false;
    }

    public int differenceFound(Difference difference) {
        if (isIgnoredDifference(difference)) {
            return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
        } else {
            return RETURN_ACCEPT_DIFFERENCE;
        }
    }

    public void skippedComparison(Node control, Node test) {
    }
}

这里需要检查属性名称是否为“id”。标准 Java DOM 功能可能会有所帮助。但我更喜欢通过正则表达式来做到这一点:

String controlNode = difference.getControlNodeDetail().getNode().toString();
controlNode .matches("^id=\".*\"")

PS 另请参阅: http://xmlunit .sourceforge.net/api/org/custommonkey/xmlunit/Difference.html

The solution is quite simple. You can configure your DifferenceEngine to handle ATTR_VALUE differences. Write custom difference listener class which implements DifferenceListener:

class IgnoreIDsDifferenceListener implements DifferenceListener {
    private static final int[] IGNORE_VALUES = new int[] {
            DifferenceConstants.ATTR_VALUE.getId(),
    };

    private boolean isIgnoredDifference(Difference difference) {
        int differenceId = difference.getId();
        for (int i=0; i < IGNORE_VALUES.length; ++i) {
            if (differenceId == IGNORE_VALUES[i]) {
                return true;
            }
        }
        return false;
    }

    public int differenceFound(Difference difference) {
        if (isIgnoredDifference(difference)) {
            return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
        } else {
            return RETURN_ACCEPT_DIFFERENCE;
        }
    }

    public void skippedComparison(Node control, Node test) {
    }
}

The required thing here is to check whether the attribute name is "id". Standard Java DOM functionality could help. But I prefer to do this by the means of regular expressions:

String controlNode = difference.getControlNodeDetail().getNode().toString();
controlNode .matches("^id=\".*\"")

P.S. See also: http://xmlunit.sourceforge.net/api/org/custommonkey/xmlunit/Difference.html

暖阳 2024-10-28 05:19:17

我将尝试实现我自己的 DifferenceListener 来处理这个问题需要(请参阅这篇文章)。很快就会发布结果。看来可行,明天发布实施。

I am gonna try to implement my own DifferenceListener to handle this need (see this post). Gonna post the result soon. It seems to work, gonna post the implementation tomorrow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文