【问题标题】:JAXB - Binding element to Set instead of ListJAXB - 将元素绑定到 Set 而不是 List
【发布时间】:2011-05-11 10:07:09
【问题描述】:

有没有办法让 JAXB 为已定义的元素生成 Collection Set 而不是 List?

例如为这个 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<Book> books = new HashSet<Book>();

【问题讨论】:

    标签: java xml jaxb


    【解决方案1】:

    我不认为可以通过自定义绑定来完成,因为根据Customizing JAXB Bindings 上的指南:

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

    但是,如果您编写自己的 xjc 插件,则可以做到这一点。看看下面的文章看看如何:Writing a plug-in for the JAXB RI is really easy

    【讨论】:

      猜你喜欢
      • 2011-12-01
      • 1970-01-01
      • 2017-05-05
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多