有没有办法让JAXB为已定义的元素生成集合集而不是列表?
例如,为此xsd生成一套书籍:
<xs:element name="Collection">
<xs:complexType>
<xs:sequence>
<xs:element name ="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="bookType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
Run Code Online (Sandbox Code Playgroud)
使用以下bindings.xml时
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="//xs:element[@name='Shop']/xs:complexType/xs:sequence/xs:element[@name='books']">
<jxb:property collectionType="java.util.HashSet" />
</jxb:bindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)
生成具有混凝土HashSet实现的书籍列表:
List<Book> books = new HashSet<Book>();
Run Code Online (Sandbox Code Playgroud)
我不认为可以使用自定义绑定来完成,因为根据自定义JAXB绑定的指南:
collectionType定义自定义值propertyCollectionType,该属性是属性的集合类型.propertyCollectionType如果指定,则可以是索引或任何实现的完全限定类名java.util.List.
但是,如果您编写了自己的xjc插件,则可能会这样做.看一下下面的文章,看看如何:为JAXB RI编写插件非常简单