本文介绍了JAXB - 要设置的绑定元素而不是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>();


推荐答案

我不认为可以用自定义绑定,因为根据:

I don't think it can be done with a custom binding, because according to the guide on Customizing JAXB Bindings:

但是,如果你写的话可能会这样做你自己的 xjc插件。请查看以下文章,了解如何:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:49
查看更多