JAXB 整数和长解析
JAXB Integer and Long parsing
我有一个包含多个数值的对象结构:
@XmlTransient
public class Person
{
private Integer age;
private Long shoeSize;
public setAge(Integer age){...}
public getAge(){return age;}
...
}
public class Child extends Person
{
private Integer readingAge;
public setReadingAge(Integer readingAge){...}
public getReadingAge(){return readingAge;}
}
public class Team
{
private ArrayList<Child> children = new Arraylist<Child>();
...
}
因此,当我编组 Child 对象时,年龄被遗漏了。 age 有可能为空,所以我不能只将它添加为 int 值。
@xmlTransient 背后的原因是我需要编组以按字母顺序编组整个文档。如果我删除瞬态,则对 person 元素进行排序,然后将子元素添加到末尾。
所以我的问题是:
无论如何,我可以在不将值默认为 0 的情况下识别年龄吗?
我可以在不设置临时标志的情况下订购整个文档吗?
我试图简化我的例子,因为真实的例子有 100 个元素
目标 XML:
<Team>
<Child>
<age>5</age>
<readingAge>25</readingAge>
<shoeSize>1</shoeSize>
</Child>
</Team>
但如果年龄为空:
<Team>
<Child>
<readingAge>25</readingAge>
<shoeSize>1</shoeSize>
</Child>
<Team>
编组 class:
Child c = new Child(5, 9, 25);
Team t = new Team();
t.getChildren().put(c);
if (getMarshaller() == null)
{
JAXBContext context = JAXBContext.newInstance(Team.class);
setMarshaller(context.createMarshaller());
}
StringWriter sw = new StringWriter();
getMarshaller().marshal(t, sw);
String xmlString = sw.toString();
So when I marshall the Child object the age is missed. There is a
possability that age can be null so i cannot just add it as an int
value.
正确,如果应该将 null
年龄编组并(稍后)再次将其解组为 null
,这就是处理它的方法。 缺少元素是 null
值的表示方式。
Can I order the document as a whole without setting the transient
flag?
是的,您可以:class Team
中的 ArrayList<Child> children
包含 ordered 对象的 Child
集合,并且它们将按此顺序编组,然后(稍后)再次解编回同一个列表。
I need the marshalling to marshall the whole document in alphabetical
order.
这不是 JAXB 会自动为您做的事情。例如,如果 children
列表应按字母顺序排序,则需要对其进行排序,例如,通过使用团队对象的字段 children
作为其(第一个)参数调用 Collections::sort
。 (我无法提供示例,因为我没有在 Person
或 Child
中看到字符串字段,甚至在 XML 示例中也没有。)
编辑
I only need the ordering where inheritance is concerned. E.g. <age>
<readingAge> <shoeSize>
同样,这不是 JAXB 会做的事情,而且这是一个非常不寻常的要求。 (目的是什么?)可能你最好的选择是覆盖 Child class 中的 Person 吸气剂,并通过用
注释 class 来定义顺序
@XmlType(propOrder = {"age","readingAge","shoeSize"})
我有一个包含多个数值的对象结构:
@XmlTransient
public class Person
{
private Integer age;
private Long shoeSize;
public setAge(Integer age){...}
public getAge(){return age;}
...
}
public class Child extends Person
{
private Integer readingAge;
public setReadingAge(Integer readingAge){...}
public getReadingAge(){return readingAge;}
}
public class Team
{
private ArrayList<Child> children = new Arraylist<Child>();
...
}
因此,当我编组 Child 对象时,年龄被遗漏了。 age 有可能为空,所以我不能只将它添加为 int 值。
@xmlTransient 背后的原因是我需要编组以按字母顺序编组整个文档。如果我删除瞬态,则对 person 元素进行排序,然后将子元素添加到末尾。
所以我的问题是: 无论如何,我可以在不将值默认为 0 的情况下识别年龄吗? 我可以在不设置临时标志的情况下订购整个文档吗? 我试图简化我的例子,因为真实的例子有 100 个元素
目标 XML:
<Team>
<Child>
<age>5</age>
<readingAge>25</readingAge>
<shoeSize>1</shoeSize>
</Child>
</Team>
但如果年龄为空:
<Team>
<Child>
<readingAge>25</readingAge>
<shoeSize>1</shoeSize>
</Child>
<Team>
编组 class:
Child c = new Child(5, 9, 25);
Team t = new Team();
t.getChildren().put(c);
if (getMarshaller() == null)
{
JAXBContext context = JAXBContext.newInstance(Team.class);
setMarshaller(context.createMarshaller());
}
StringWriter sw = new StringWriter();
getMarshaller().marshal(t, sw);
String xmlString = sw.toString();
So when I marshall the Child object the age is missed. There is a possability that age can be null so i cannot just add it as an int value.
正确,如果应该将 null
年龄编组并(稍后)再次将其解组为 null
,这就是处理它的方法。 缺少元素是 null
值的表示方式。
Can I order the document as a whole without setting the transient flag?
是的,您可以:class Team
中的 ArrayList<Child> children
包含 ordered 对象的 Child
集合,并且它们将按此顺序编组,然后(稍后)再次解编回同一个列表。
I need the marshalling to marshall the whole document in alphabetical order.
这不是 JAXB 会自动为您做的事情。例如,如果 children
列表应按字母顺序排序,则需要对其进行排序,例如,通过使用团队对象的字段 children
作为其(第一个)参数调用 Collections::sort
。 (我无法提供示例,因为我没有在 Person
或 Child
中看到字符串字段,甚至在 XML 示例中也没有。)
编辑
I only need the ordering where inheritance is concerned. E.g.
<age> <readingAge> <shoeSize>
同样,这不是 JAXB 会做的事情,而且这是一个非常不寻常的要求。 (目的是什么?)可能你最好的选择是覆盖 Child class 中的 Person 吸气剂,并通过用
注释 class 来定义顺序@XmlType(propOrder = {"age","readingAge","shoeSize"})