如何使 JAXB 识别 @XmlElement(default='something') 注释参数?

发布于 2024-10-10 09:48:24 字数 831 浏览 0 评论 0 原文

未能使用 JAXB 2.1 RI 生成具有固定元素值的 XML。 我们使用 xjc 生成 XML 绑定代码并编组结果。

它适用于使用 和 JAXB 自定义属性 fixedAttributeAsConstantProperty 的属性。 对于元素,我们认为没有办法做到同样的事情。或者有吗?

作为解决方法,我们使用 ,将其转换为 @XmlElement(default='something')。现在我的猜测是,您可以告诉编组器以某种方式解释 default 参数并生成具有相应内容的元素,例如 something

我查看了标准和 RI 供应商特定的编组器配置属性,但没有找到有用的东西。 似乎有一个 xjc 插件可以执行类似的操作[1],但如果没有标准的 JAXB 方法来执行此操作,我会感到有点惊讶。

非常感谢任何指点,谢谢。

[1] http:// /fisheye5.cenqua.com/browse/~raw,r=1.5/jaxb2-commons/www/default-value/index.html

Having no luck in generating XML with fixed element values using JAXB 2.1 RI.
We're generating XML bound code using xjc and marshall the results.

It works for attributes using <xs:attribute fixed='something'/> and the JAXB customization property fixedAttributeAsConstantProperty.
For elements we figured that there was no way to do the same. Or is there?

As a workaround, we used <xs:element default='something'/> which is turned into @XmlElement(default='something'). Now my guess is that you can tell the marshaller somehow to interpret the default parameter and generate an element with the corresponding content, like <element>something</element>.

I've looked at the standard and RI vendor specific marshaller configuration properties without finding something useful.
There seems to be an xjc plug-in that does something similar[1], but I'd be kinda surprised if there is no standard JAXB way to do so.

Any pointers are much appreciated, thanks.

[1] http://fisheye5.cenqua.com/browse/~raw,r=1.5/jaxb2-commons/www/default-value/index.html

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

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

发布评论

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

评论(2

静赏你的温柔 2024-10-17 09:48:24

XML 模式规范仅处理解组,即确定源 XML 表示的验证后信息集。

JAXB 似乎在其对默认值的支持中反映了这一点,即主要用于读取。例如,对默认属性的支持不会扩展到写入默认值,而只是读取(或使用 getter 从 Java 访问),

据我所知,解决方案的最佳选择是扩展默认值插件来处理固定元素,如下所示好吧,通过将场地定为决赛并移除二传手。

插件代码非常简单,真的!

The XML schema specification deals with unmarshalling only, i.e. determining the post validation infoset of the source XML representation.

JAXB appears to mirror this in its support for defaults, i.e. for reading mostly. For instance, the support for default attributes does not extend to writing defaults, just to reading (or accessing from Java using the getter)

As I see it, the best bet for a solution is to extend the default value plugin to handle fixed elements as well, by making the field final and removing the setter.

The code for the plugin is very straightforward, really!

二智少女猫性小仙女 2024-10-17 09:48:24

有一个 maven-plugin 可以用来生成常量: https://github.com/fbdo/ jaxb2-固定值

There is an maven-plugin one can use to generate constants: https://github.com/fbdo/jaxb2-fixed-value

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