[英]JAXB Annotations
I need a little help with JAXB Annotations and I couldn't find good doc's helping me figure this out. 我需要一些JAXB Annotations的帮助,我找不到好的文档帮助我解决这个问题。
I have a class that I want to marshal into XML. 我有一个我想要编组成XML的类。 My Class looks like this:
我的班级看起来像这样:
@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;
...
And the recall class looks like this: 召回课看起来像这样:
@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;
...
So it produces this XML output: 所以它产生了这个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>
But what I want the ArrayList to display differently, like this: 但是我希望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>
Any idea how I can do this? 知道我怎么能这样做吗? I think the problem is with my schema, but I use this for the marshalling:
我认为问题出在我的架构上,但我用它进行编组:
JAXBContext jc = JAXBContext.newInstance(Car.class);
Marshaller marsh = jc.createMarshaller();
marsh.marshal(car, out);
Any ideas how to fix this? 任何想法如何解决这一问题? Thanks!
谢谢!
Try this: 试试这个:
@XmlRootElement(name="car")
...
public class Car {
...
@XmlElementWrapper(name="recalls") // this name=... can be omitted, as it
// is the same as the field name
@XmlElement(name="recall")
private ArrayList<Recall> recalls;
}
From the documentation : 从文档 :
XmlElementWrapper : Generates a wrapper element around XML representation.
XmlElementWrapper :围绕XML表示生成包装元素。 This is primarily intended to be used to produce a wrapper XML element around collections.
这主要用于围绕集合生成包装器XML元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.