Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB - Binding element to Set instead of List

Tags:

java

xml

jaxb

Is there a way to make JAXB generate the Collection Set instead of List for an defined element?

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>

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>

A List of books with a concret HashSet implementation is generated:

List<Book> books = new HashSet<Book>();
like image 649
jonbros Avatar asked May 11 '11 10:05

jonbros


People also ask

What is JAXB binding file?

JAXB is an XML-to-Java binding technology that enables transformation between schema and Java objects and between XML instance documents and Java object instances. JAXB technology consists of a runtime API and accompanying tools that simplify access to XML documents.

What is XJC simple?

XJC is a Java SE tool that compiles an XML schema file into fully annotated Java classes. It is distributed within the JDK package and is located at /bin/xjc path.

What is XJB file in JAXB?

xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

When parsing XML What is the default value of choice content property in the global binding attribute?

choiceContentProperty can be either true, false, 1, or 0. The default value is false.


1 Answers

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

collectionType defines the customization value propertyCollectionType, which is the collection type for the property. propertyCollectionType if specified, can be either indexed or any fully-qualified class name that implements java.util.List.

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

like image 124
dogbane Avatar answered Sep 18 '22 19:09

dogbane