将字段/属性编组到具有附加 xml 属性 name=propertyName 的 xml 元素
我有一个像这样的java对象:
public class Person {
private String firstName = "Harry";
private String lastName = "Hacker";
private int age = 30;
}
我想将其编组到以下xml中:
<attribute xsi:type="someType" name="Person">
<attribute xsi:type="CustomStringType" name="firstName">
<value>Harry</value>
</attribute>
<attribute xsi:type="CustomStringType" name="lastName">
<value>Hacker</value>
</attribute>
<attribute xsi:type="CustomIntType" name="age">
<value>30</value>
</attribute>
</attribute>
所以我想做的是,我希望Person(以及人本身)中的所有对象都是xml元素“属性”并且具有这个 xml 元素具有属性“name”,它表示字段的名称(假设 Person 用作此处未显示的类中的字段)。此外,我想整理“原始类型”以使“值”元素具有适当的值。 这可以使用 JaxB 来完成吗?如果是的话怎么办?当要求必须很容易(即只需向新字段添加一些注释)来向 xml/类结构添加新的“属性”(即字段(例如人员的地址))时,您会看到哪些其他解决方案?
I have a java object like:
public class Person {
private String firstName = "Harry";
private String lastName = "Hacker";
private int age = 30;
}
which I would like to marshal into the following xml:
<attribute xsi:type="someType" name="Person">
<attribute xsi:type="CustomStringType" name="firstName">
<value>Harry</value>
</attribute>
<attribute xsi:type="CustomStringType" name="lastName">
<value>Hacker</value>
</attribute>
<attribute xsi:type="CustomIntType" name="age">
<value>30</value>
</attribute>
</attribute>
so what I want to do, I want all objects in the Person (and the person itself) to be of xml-element "attribute" and to have this xml-element with an attribute "name" which represents the name of the field (lets assume Person is used as a field in class not shown here). Additionally I want to marshal the "primitive types" to have the "value" element with the appropriate value.
Can this be done using JaxB? If yes how? What other solutions do you see when the requirement is that it has to be easy (i.e. just add some annotations to the new field) to add new "attributes" (i.e. fields (e.g. an address of the person) to the xml/class structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否检查过实现自己的自定义 XmlAdapter,并使用 @XmlJavaTypeAdapter 注释?
它允许您定义自己的定制序列化策略。
Have you checked out implementing your own custom XmlAdapter, and anotate your Person type with @XmlJavaTypeAdapter anotation?
It allows you to define you own customized serialization strategy.