Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>UPDATE</h2> <p>There are XJC plug-ins that people have written to generate the <code>@XmlElementWrapper</code> annotation instead of having the extra wrapper class.</p> <ul> <li><a href="https://github.com/dmak/jaxb-xew-plugin">https://github.com/dmak/jaxb-xew-plugin</a></li> </ul> <hr> <h2>ORIGINAL ANSWER</h2> <p>Alternative you could create the class with the <code>@XmlElementWrapper</code> yourself and have the generated classes reference it by doing the following:</p> <p><strong>Document</strong></p> <p>You could handcraft your own <code>Document</code> class to get the desired behaviour. You can get the behaviour you are looking for by leveraging the <code>@XmlElementWrapper</code> annotation to get a grouping element.</p> <pre class="lang-java prettyprint-override"><code>package forum18247182; import java.util.*; import javax.xml.bind.annotation.*; public class Document { private List&lt;Foos.Foo&gt; foos = new ArrayList&lt;Foos.Foo&gt;(); @XmlElementWrapper @XmlElement(name="foo") public List&lt;Foos.Foo&gt; getFoos() { return foos; } } </code></pre> <p><strong>XML Schema (schema.xsd)</strong></p> <p>Here is an expanded XML schema based on your fragment that I will use.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/schema" xmlns="http://www.example.org/schema" elementFormDefault="qualified"&gt; &lt;xs:element name="document" type="Document"/&gt; &lt;xs:complexType name="Document"&gt; &lt;xs:sequence&gt; &lt;xs:element name="foos" type="Foos"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Foos"&gt; &lt;xs:sequence&gt; &lt;xs:element name="foo" minOccurs="0" maxOccurs="unbounded"&gt; &lt;xs:complexType&gt; &lt;xs:all&gt; &lt;xs:element name="bar" type="xs:string" /&gt; &lt;xs:element name="baz" type="xs:string" /&gt; &lt;/xs:all&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p><strong>Leverage Existing Class When Generating Java Model from XML Schema (binding.xml)</strong></p> <p>We will use an external binding file to indicate that during class generation we wish to use our existing class for the complex type called <code>Document</code>.</p> <pre><code>&lt;jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"&gt; &lt;jxb:bindings schemaLocation="schema.xsd"&gt; &lt;jxb:bindings node="//xs:complexType[@name='Document']"&gt; &lt;jxb:class ref="forum18247182.Document"/&gt; &lt;/jxb:bindings&gt; &lt;/jxb:bindings&gt; &lt;/jxb:bindings&gt; </code></pre> <p><strong>XJC Call</strong></p> <p>We will use the <code>-b</code> option to specify our binding file. We will also use the <code>-p</code> option to force the package name of the generated classes to match that of our <code>Document</code> class. We could also have made the package name of our <code>Document</code> class match the package name that results from generating classes from the XML schema.</p> <pre><code>xjc -b binding.xml -p forum18247182 schema.xsd </code></pre> <p><strong>Demo Code</strong></p> <pre class="lang-java prettyprint-override"><code>package forum18247182; import javax.xml.bind.*; import javax.xml.transform.stream.StreamSource; import forum18247182.Foos.Foo; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance("forum18247182"); Unmarshaller unmarshaller = jc.createUnmarshaller(); StreamSource xml = new StreamSource("src/forum18247182/input.xml"); Document document = unmarshaller.unmarshal(xml, Document.class).getValue(); for(Foo foo : document.getFoos()) { System.out.println(foo); } } } </code></pre> <p><strong>Output</strong></p> <p>Below is the output from running the demo code:</p> <pre><code>forum18247182.Foos$Foo@51f3336e forum18247182.Foos$Foo@35b5a4ca </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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