有没有一种简单的方法可以使用 XMLBeans 进行 XML 属性比较?

发布于 2024-12-04 08:51:42 字数 412 浏览 3 评论 0原文

假设有一个带有一组分层属性的 bean,XMLBeans 是否提供了一种方法来确定该 bean 是否具有这些属性的子集的某些值?我能想到的解决此问题的最佳方法是创建另一个具有您所寻求的属性和值集的 bean,并查看您的 bean 是否包含与您设置的属性相同的属性(以及这些属性的值) 。

例如,假设您有一个 XML 人员列表,并且您想要查找棕色头发的人员。您可以这样设置一个 bean:

<person hair="brown" />

并在列表中查找基本上包含该 bean 作为子集的项目。我可以通过递归地迭代测试 bean(在本例中为棕色头发的人)的属性层次结构来完成此操作,并检查列表中的每个 bean 是否包含该属性并具有该值。但这似乎可能是一项常见任务,我想了解 XMLBeans 库是否已经提供了此功能。

Supposing that one has a bean with a set of hierarchical attributes, does XMLBeans provide a way to determine whether that bean has certain values for a subset of those attributes? The best way I could come up with to solve this issue is to create another bean with the set of attributes and values that you seek, and see if your bean contains the same attributes (and values for those attributes) as the one you set up.

For example, consider you have a list of XML people, and you want to find people with brown hair. You could set up a bean as such:

<person hair="brown" />

and find the items in your list which essentially contain that bean as a subset. I could do this by recursively iterating through the attribute hierarchy of the test bean (in this case, the person with brown hair), and check if each bean in the list contains that attribute, and has that value for it. But this seems like it might be a common task, and I wanted to find out if the XMLBeans library provides this functionality already.

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

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

发布评论

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

评论(1

捎一片雪花 2024-12-11 08:51:42

我不知道什么是层次属性,但我知道您可以使用 XPath: 查询原始 XML:

//person[@hair='brown']

以获取“hair”属性等于“brown”的所有 person 元素。我认为您可以针对 XMLBeans XMLObject 执行 XPath 表达式(或者是 XQuery?,但这几乎是同一件事),所以,是的,XMLBeans 已经具有该功能了。据我记得,您会得到一个 XMLObject 列表,但我不知道这些是否可以转换为它们特定的映射 Java 类。奇怪的野兽,XMLBeans!我们转向 JAXB,并没有后悔。

I don't know what a hierarchical attribute is, but I know that you can query the original XML using XPath:

//person[@hair='brown']

to get all person elements with a 'hair' attribute equal to 'brown'. And I think you can execute XPath expressions against the XMLBeans XMLObject (or was it XQuery?, but that would be almost the same thing), so, yes, XMLBeans kind of have that functionality already. As far as I remember, you'd get a list of XMLObjects back, but I dont know if those could be cast to their particular mapped Java classes or not. Strange beast, XMLBeans! We moved on to JAXB and have no regrets.

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