JAXB Annotations
2022-09-03 17:56:16
我需要一些关于JAXB注释的帮助,但我找不到好的文档来帮助我解决这个问题。
我有一个类,我想将其封送到 XML 中。我的类如下所示:
@XmlRootElement(name="car")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
"vid",
"make",
"model",
"recalls",
"engSpec"
})
public class Car {
@XmlElement(name="vid", required=true)
private String vid;
@XmlElement(name="make", required=true)
private String make;
@XmlElement(name="model", required=true)
private String model;
@XmlElement(name="recalls", required=true)
private ArrayList<Recall> recalls;
@XmlElement(name="engSpec", required=true)
private EngSpec engSpec;
...
召回类如下所示:
@XmlRootElement(name = "recall")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
"type",
"date"
})
public class Recall {
@XmlElement(name="type", required=true)
private String type;
@XmlElement(name="date", required=true)
private String date;
...
因此,它生成以下 XML 输出:
<car>
<vid>vid</vid>
<make>make</make>
<model>model</model>
<recalls>
<type>Recall1</type>
<date>01/01/11</date>
</recalls>
<recalls>
<type>Recall2</type>
<date>01/01/11</date>
</recalls>
<engSpec>
<power>200HP</power>
<size>size</size>
</engSpec>
</car>
但是我希望 ArrayList 以不同的方式显示,如下所示:
<car>
<vid>vid</vid>
<make>make</make>
<model>model</model>
<recalls>
<recall>
<type>Recall1</type>
<date>01/01/11</date>
</recall>
<recall>
<type>Recall2</type>
<date>01/01/11</date>
</recall>
</recalls>
<engSpec>
<power>200HP</power>
<size>size</size>
</engSpec>
</car>
任何想法,我怎么能做到这一点?我认为问题出在我的架构上,但我用它来进行编组:
JAXBContext jc = JAXBContext.newInstance(Car.class);
Marshaller marsh = jc.createMarshaller();
marsh.marshal(car, out);
任何想法如何解决这个问题?谢谢!