2011-05-11 35 views
5

有沒有辦法讓JAXB爲定義的元素生成Collection集合而不是List?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> 

當採用下面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實現的List產生:

List<Book> books = new HashSet<Book>(); 

回答

6

我不認爲它可以用自定義綁定來完成,因爲根據上Customizing JAXB Bindings指南:

collectionType定義 定製值 propertyCollectionType,這是該屬性的 集合類型。 propertyCollectionType如果指定, 可以索引或 完全合格的類名 實施java.util.List

但是,如果你寫你自己的XJC插件有可能做到這一點。看看下面的文章,看看如何:Writing a plug-in for the JAXB RI is really easy

相關問題