JAXB - 将元素绑定到 Set 而不是 List

7
有没有办法让JAXB为一个已定义的元素生成Collection Set而不是List呢?
例如,为这个xsd生成一组书籍的Set:
<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<Book> books = new HashSet<Book>();
1个回答

6
我认为使用自定义绑定是不可能完成的,因为根据JAXB绑定自定义指南:

collectionType 定义了定制值 propertyCollectionType,它是属性的集合类型。如果指定了 propertyCollectionType,它可以是索引或任何实现 java.util.List 的完全限定类名。

然而,如果您编写自己的xjc插件,则可能可以实现此操作。请参阅以下文章了解详细信息:编写JAXB RI插件非常容易

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接