jaxb解组xml元素以包含一些字段

时间:2018-08-01 15:58:33

标签: java xml jaxb

我有一些课程:

class Location  {   

    private Long id;   

    private String roomTitle;  

    protected Specialization specialization;
}

public class Specialization  {

    private Long id;

    private boolean archived;

    private String title;
}

和xml:

<location id="10"> 
  <roomTitle>string</roomTitle>
  <specializationId>10</specializationId>
</location>

我可以将specializationId编组为Location.specialization.id吗?

1 个答案:

答案 0 :(得分:0)

我不了解jaxb,但SimpleXml可以做到。

首先,我们需要添加一些注释:

Series.cat.catgeories

请注意,public class Location { private Long id; private String roomTitle; @XmlName("specializationId") protected Specialization specialization; } public class Specialization { @XmlTextNode private Long id; private Boolean archived; private String title; } 现在是一个布尔类。常规布尔值不能为null。接下来,我们将xml序列化为Location类。

archived

然后我们可以打印值

final SimpleXml simple = new SimpleXml();
final Location l = simple.fromXml(xml, Location.class);

这将打印

System.out.println(l.specialization.id);

SimpleXml位于Maven中心

10