我建议你这个解决方案.通过这种方式,您可以根据需要添加任意数量的级别.
Root.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "root", propOrder = {
"items"
})
@XmlRootElement(name = "root")
public class Root
implements Serializable
{
private final static long serialVersionUID = 1234567890L;
@XmlElement(name = "item")
protected List items;
/**
* Gets the value of the items property.
*
*
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a set
method for the items property.
*
*
* For example, to add a new item, do as follows:
*
* getItems().add(newItem);
*
*
*
*
* Objects of the following type(s) are allowed in the list
* {@link Item }
*
*
*/
public List getItems() {
if (items == null) {
items = new ArrayList();
}
return this.items;
}
}
Item.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "item", propOrder = {
"content"
})
@XmlRootElement(name = "item")
public class Item
implements Serializable
{
private final static long serialVersionUID = 1234567890L;
@XmlMixed
@XmlAnyElement(lax = true)
protected List content;
@XmlAttribute(name = "num")
protected String num;
@XmlAttribute(name = "label")
protected String label;
@XmlAttribute(name = "type")
protected String type;
/**
* Gets the value of the content property.
*
*
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a set
method for the content property.
*
*
* For example, to add a new item, do as follows:
*
* getContent().add(newItem);
*
*
*
*
* Objects of the following type(s) are allowed in the list
* {@link String }
* {@link Object }
*
*
*/
public List getContent() {
if (content == null) {
content = new ArrayList();
}
return this.content;
}
/**
* Recupera il valore della proprietà num.
*
* @return
* possible object is
* {@link String }
*
*/
public String getNum() {
return num;
}
/**
* Imposta il valore della proprietà num.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setNum(String value) {
this.num = value;
}
/**
* Recupera il valore della proprietà label.
*
* @return
* possible object is
* {@link String }
*
*/
public String getLabel() {
return label;
}
/**
* Imposta il valore della proprietà label.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setLabel(String value) {
this.label = value;
}
/**
* Recupera il valore della proprietà type.
*
* @return
* possible object is
* {@link String }
*
*/
public String getType() {
return type;
}
/**
* Imposta il valore della proprietà type.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setType(String value) {
this.type = value;
}
}
我用过这个XSD
Main.java
public static void main(String[] args) throws Throwable {
JAXBContext jc = JAXBContext.newInstance(Root.class, Item.class);
Root r = new Root();
Item i = new Item();
i.setLabel("This is a LIST item");
i.setType("List");
Item i2 = new Item();
i2.setLabel("Upper");
i2.setType("string");
i2.getContent().add("ABC");
i.getContent().add(i2);
Item i3 = new Item();
i3.setLabel("Lower");
i3.setType("string");
i3.getContent().add("abc");
i.getContent().add(i3);
Item i4 = new Item();
i4.setNum("1");
i4.setType("list");
Item i5 = new Item();
i5.setLabel("a");
i5.setType("other");
i5.getContent().add("aaaaa");
i4.getContent().add(i5);
i.getContent().add(i4);
r.getItems().add(i);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.marshal(r, System.out);
}
产量
ABC
abc
aaaaa