编组具有对象字段的对象
不确定标题是否有意义。 我想使用 JAXB 封送一个如下所示的对象:
@XmlRootElement(name = "subscriptionRequest")
public class RegistrationRequest {
private Long id;
private RegistrationSource registrationSource;
}
RegistrationSource 对象:
public class RegistrationSource {
private Integer id;
private String code;
}
我想创建一个具有以下布局的 xml:
<subscriptionRequest registrationSource="0002">
...
</subscriptionRequest>
其中,registrationSource 属性值是来自 RegistrationSource 对象的代码字段值。
我需要使用哪些 xml 注释?
Not sure if the title makes any sense.
I have an object that I want to marshal using JAXB that looks like this:
@XmlRootElement(name = "subscriptionRequest")
public class RegistrationRequest {
private Long id;
private RegistrationSource registrationSource;
}
The RegistrationSource object:
public class RegistrationSource {
private Integer id;
private String code;
}
I want to create an xml that has the following layout:
<subscriptionRequest registrationSource="0002">
...
</subscriptionRequest>
where the registrationSource attribute value is the code field value from the RegistrationSource object.
What xml annotations do I need to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
registrationSource
上的@XmlAttribute
,code
上的@XmlValue
。请注意,在这种情况下,您还应该在RegistrationSource
的其他字段上使用@XmlTransient
,例如id
编辑:这有效:
-
@XmlAttribute
onregistrationSource
,@XmlValue
oncode
. Note that in this case you also should have@XmlTransient
on other fields ofRegistrationSource
, such asid
EDIT: This works:
-
如果您想使用某些工具自动生成此类,请尝试以下操作 -
使用 Trang 等工具从 xml 生成 xsd,然后使用从 xsd 生成 java 文件jaxb。生活会简单得多:)
If you want to generate this class automatically using some tools , then try this -
Generate xsd from your xml using tools like Trang, and then generate java file from xsd using jaxb. Life would be much simpler :)
蹩脚的方法是
向您的
RegistrationSource
添加类似的内容 - 但必须有一种更优雅的方法......The lame approach would be to add something like
to your
RegistrationSource
-- but there must be a more elegant way...