默认情况下,属性将是一个List,底层实现将是一个ArrayList。当然,您可以使用JAXB自定义来更改底层实现,或者使用自己的类并具有类型为ArrayList的属性(尽管出于其他答案中提到的原因,这很少是好主意)。
默认的JAXB生成:
给定您的XML模式:
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<complexType name="BookShelf">
<sequence>
<choice minOccurs="0" maxOccurs="unbounded">
<element name="newBook" type="string"/>
<element name="oldBook" type="string"/>
</choice>
</sequence>
</complexType>
</schema>
使用以下命令行:
xjc -d out your-schema.xsd
JAXB 将生成以下类:
package generated;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BookShelf", propOrder = {
"newBookOrOldBook"
})
public class BookShelf {
@XmlElementRefs({
@XmlElementRef(name = "newBook", type = JAXBElement.class),
@XmlElementRef(name = "oldBook", type = JAXBElement.class)
})
protected List<JAXBElement<String>> newBookOrOldBook;
public List<JAXBElement<String>> getNewBookOrOldBook() {
if (newBookOrOldBook == null) {
newBookOrOldBook = new ArrayList<JAXBElement<String>>();
}
return this.newBookOrOldBook;
}
}
自定义生成
JAXB默认情况下,属性类型为List,底层实现为ArrayList。如果您希望控制底层实现,可以使用外部绑定文件,例如:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="f3.xsd">
<jxb:bindings node="//xs:complexType[@name='BookShelf']/xs:sequence/xs:choice">
<jxb:property collectionType="java.util.LinkedList"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
以下是 XJC 的调用:
xjc -d out -b binding.xml your-schema.xsd
为了获得以下类:
package generated;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BookShelf", propOrder = {
"newBookOrOldBook"
})
public class BookShelf {
@XmlElementRefs({
@XmlElementRef(name = "oldBook", type = JAXBElement.class),
@XmlElementRef(name = "newBook", type = JAXBElement.class)
})
protected List<JAXBElement<String>> newBookOrOldBook = new LinkedList<JAXBElement<String>>();
public List<JAXBElement<String>> getNewBookOrOldBook() {
if (newBookOrOldBook == null) {
newBookOrOldBook = new LinkedList<JAXBElement<String>>();
}
return this.newBookOrOldBook;
}
}
使用自己的类:
您也可以使用自己的类,并拥有一个类型为ArrayList的属性(尽管出于其他答案中提到的原因,这很少是一个好主意)。
package com.example;
import java.util.ArrayList;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BookShelf", propOrder = {
"newBookOrOldBook"
})
public class BookShelf {
@XmlElementRefs({
@XmlElementRef(name = "oldBook", type = JAXBElement.class),
@XmlElementRef(name = "newBook", type = JAXBElement.class)
})
protected ArrayList<JAXBElement<String>> newBookOrOldBook ;
public ArrayList<JAXBElement<String>> getNewBookOrOldBook() {
if (newBookOrOldBook == null) {
newBookOrOldBook = new ArrayList<JAXBElement<String>>();
}
return this.newBookOrOldBook;
}
}
更多信息: