比较两个 XmlBeans 对象是否相等

发布于 2024-12-10 12:35:32 字数 839 浏览 0 评论 0原文

我有一个名为 SynonymsRequest 的 XML Beans 接口:

public interface SynonymsRequest extends org.apache.xmlbeans.XmlObject {...}

我想测试 SynonymsRequest 的两个实例是否相等:

SynonymsRequest s1 = SynonymsRequest.Factory.newInstance();
s1.setQueryText("blub");
s1.setRequesterId(BigInteger.valueOf(1));       
SynonymsRequest s2 = SynonymsRequest.Factory.newInstance();
s2.setQueryText("guck");
s2.setRequesterId(BigInteger.valueOf(1));

我尝试了以下操作:

  1. assertTrue(s1.equals(s2)); =>断言未通过
  2. assertEquals(0, s1.compareTo(s2)); =>抛出 ClassCastException
  3. assertEquals(0, s1.compareValue(s2)); =>;断言未通过(返回2,不可比较)
  4. assertTrue(s1.valueEquals(s2)); =>无论两个实例是否相等,总是返回 true

那么执行此操作的正确方法是什么?

I've an XML Beans Interface called SynonymsRequest with:

public interface SynonymsRequest extends org.apache.xmlbeans.XmlObject {...}

I want to test two instances of SynonymsRequest for equality:

SynonymsRequest s1 = SynonymsRequest.Factory.newInstance();
s1.setQueryText("blub");
s1.setRequesterId(BigInteger.valueOf(1));       
SynonymsRequest s2 = SynonymsRequest.Factory.newInstance();
s2.setQueryText("guck");
s2.setRequesterId(BigInteger.valueOf(1));

I've tried the following:

  1. assertTrue(s1.equals(s2)); => assertion does not pass
  2. assertEquals(0, s1.compareTo(s2)); => throws ClassCastException
  3. assertEquals(0, s1.compareValue(s2)); => assertion does not pass (returns 2, not compareable)
  4. assertTrue(s1.valueEquals(s2)); => always returns true, no matter if the two instances are equal

So what is the proper way of doing this?

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

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

发布评论

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

评论(4

荒人说梦 2024-12-17 12:35:32

如果它不影响程序的性能,您可以像这样比较它们:

assertTrue(s1.xmlText().equals(s2.xmlText()));

否则,我想您将不得不编写自己的自定义比较器。

If it doesn't impact the performance of your program, you could compare them like this:

assertTrue(s1.xmlText().equals(s2.xmlText()));

Otherwise, I guess you will have to write your own custom comparator.

夏至、离别 2024-12-17 12:35:32

据我了解,比较仅比较两个简单的值。它不能推导出你想要的比较算法。

或者我不明白你的意思到底是什么?

As I understand, the comparison compares two simple values only. It cannot deduct your desired comparison algorithm.

Or I don't understand what exactly do you mean?

过期情话 2024-12-17 12:35:32

XmlBeans 不支持深度比较,因此您必须编写自己的比较。不久前,开发邮件列表上有一个关于模式感知比较的主题,但我不确定它会发生什么:

http://www.mail-archive.com/[电子邮件受保护]/msg01960.html

XmlBeans doesn't support a deep comparison so you'll have to write your own. There was a thread on the dev mailing list a while ago about a schema-aware comparison, but I'm not sure anything became of it:

http://www.mail-archive.com/[email protected]/msg01960.html

没有伤那来痛 2024-12-17 12:35:32

不久前注意到这一点 - 如果两个对象在创建时生成了 toString() 方法,那么您可以在对象的 toString() 方法上使用 .equals 。这些可以相对轻松地进行比较,因为它们将检查输出 xml 是否等效。

Noticed this a while back - if the two objects have toString() methods generated when they were made, then you can to an .equals on the toString() methods of the objects. These can be compared with relativ ease, since they will check if the output xml is equivalent.

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