JAXB - 要设置的绑定元素而不是List [英] JAXB - Binding element to Set instead of List
问题描述
有没有办法让JAXB为已定义的元素生成Collection Set而不是List?
Is there a way to make JAXB generate the Collection Set instead of List for an defined element?
例如为此xsd生成一套书籍:
For example generating a Set of books for this 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>
使用时以下bindings.xml
When using the following 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>
生成具有混凝土HashSet实施的书籍清单:
A List of books with a concret HashSet implementation is generated:
List<Book> books = new HashSet<Book>();
推荐答案
我不认为可以用自定义绑定,因为根据自定义JAXB绑定的指南:
I don't think it can be done with a custom binding, because according to the guide on Customizing JAXB Bindings:
collectionType
定义
自定义值
propertyCollectionType
,这是该属性的
集合类型。
propertyCollectionType
如果指定,
可以是索引或
完全限定的类名,
实现java.util.List
。
collectionType
defines the customization valuepropertyCollectionType
, which is the collection type for the property.propertyCollectionType
if specified, can be either indexed or any fully-qualified class name that implementsjava.util.List
.
但是,如果你写的话可能会这样做你自己的 xjc插件。请查看以下文章,了解如何:为JAXB RI编写插件真的很容易
However, it might be possible to do this if you wrote your own xjc plugin. Take a look at the following article to see how: Writing a plug-in for the JAXB RI is really easy
这篇关于JAXB - 要设置的绑定元素而不是List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!