默认情况下,该属性是一个 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;
}
}
更多信息: