Note that there are some explanatory texts on larger screens.

plurals
  1. PODealing with JAXB Collections
    text
    copied!<p>I am trying to unmarshall the following XML using JAXB:</p> <pre><code>&lt;Works&gt; &lt;Work&gt; &lt;Composers&gt; &lt;Composer&gt; &lt;Name&gt;A name&lt;/Name&gt; &lt;/Composer&gt; &lt;Composer&gt; &lt;Name&gt;A name 2&lt;/Name&gt; &lt;/Composer&gt; &lt;/Composers&gt; &lt;/Work&gt; &lt;/Works&gt; </code></pre> <p>I have generated all of the classes using XJC. If I want to access the Composers collection, I have to do this:</p> <pre><code> List&lt;Composer&gt; composers = work.getComposers().getComposer(); </code></pre> <p>Is there any way I can do the following instead?</p> <pre><code> List&lt;Composer&gt; composers = work.getComposers(); </code></pre> <p>I appreciate the need for a Composers object as it derived from the XML, but when dealing in Java, having an intermediate POJO that stores the collections seems a bit redundant.</p> <p>My XSD is:</p> <p> </p> <pre><code>&lt;xsd:complexType name="Works"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="Work" type="Work" maxOccurs="unbounded" minOccurs="0"&gt;&lt;/xsd:element&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="Work"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="Composers" type="Composers"&gt;&lt;/xsd:element&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="Composers"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="Composer" type="Composer" maxOccurs="unbounded" minOccurs="0"&gt;&lt;/xsd:element&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="Composer"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="Name" type="xsd:string"&gt;&lt;/xsd:element&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload