如何在 JAXB 中取消/编组属性元素?
看起来这应该是相当明显的,但我找不到任何关于如何简单地(取消)编组也具有使用 JAXB 属性的 XML 元素的信息。
换句话说,我了解如何使用 JAXB 注释来处理:
<element>foo</element>
我了解如何使用 JAXB 与:
<element attribute1="bar" attribute2="foo" />
但我不知道如何处理类似的事情:
<id_address href="/addresses/18">18></id_address_delivery>
<id_cart href="/carts/111">111</id_cart>
<id_currency href="/currencies/13">13></id_currency>
感觉我应该能够做这样的事情(我正在使用 Scala 中的 JAXB):
@XmlElement
@BeanProperty
var idAddress: XLink = _
@XmlElement
@BeanProperty
var idCart: XLink = _
@XmlElement
@BeanProperty
var idCurrency: XLink = _
然后将 XLink
定义为:
@XmlAccessorType(XmlAccessType.FIELD)
class XLink {
@XmlAttribute
@BeanProperty
var href: String = _
@Xml???
@BeanProperty // ID is the container
var id: Long = _
}
但显然这不起作用,因为 id
不应该是子元素 - 它只是父节点的值。
这是一个常见的用例:我错过了什么?为了完整起见,我使用 Scala 中的 JAXB (MOXy)。
It seems like this should be fairly obvious, but I can't find anything on how to simply (un)marshall XML elements which also have attributes using JAXB.
In other words, I understand how to use JAXB annotations to work with:
<element>foo</element>
And I understand how to use JAXB with:
<element attribute1="bar" attribute2="foo" />
But I can't figure out how to approach something like:
<id_address href="/addresses/18">18></id_address_delivery>
<id_cart href="/carts/111">111</id_cart>
<id_currency href="/currencies/13">13></id_currency>
It feels like I should be able to do something like this (I'm using JAXB from Scala):
@XmlElement
@BeanProperty
var idAddress: XLink = _
@XmlElement
@BeanProperty
var idCart: XLink = _
@XmlElement
@BeanProperty
var idCurrency: XLink = _
And then define XLink
as:
@XmlAccessorType(XmlAccessType.FIELD)
class XLink {
@XmlAttribute
@BeanProperty
var href: String = _
@Xml???
@BeanProperty // ID is the container
var id: Long = _
}
But obviously this doesn't work, because id
shouldn't be a sub-element - it's simply the value of the parent node.
This is such a common use case: what am I missing? For completeness, I'm using JAXB (MOXy) from Scala.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要
@XmlValue
。有关详细信息,请参阅此处。
I think you want
@XmlValue
.See here for more info.