title: JAXB: How to marshal objects in lists?
body: <p>Perhaps a stupid question: I have a <code>List</code> of type <code>&lt;Data&gt;</code> which I want to marshal into a XML file. This is my class <code>Database</code> containing an <code>ArrayList</code>...</p>
  
  <pre class="lang-java prettyprint-override"><code>@XmlRootElement
  public class Database
  {
      List&lt;Data&gt; records = new ArrayList&lt;Data&gt;();
  
      public List&lt;Data&gt; getRecords()                   { return records; }
      public void       setRecords(List&lt;Data&gt; records) { this.records = records; }
  }
  </code></pre>
  
  <p>...and this is class Data:</p>
  
  <pre class="lang-java prettyprint-override"><code>// @XmlRootElement
  public class Data 
  {
      String name;
      String address;
  
      public String getName()            { return name;      }
      public void   setName(String name) { this.name = name; }
  
      public String getAddress()               { return address;         }
      public void   setAddress(String address) { this.address = address; }
  }
  </code></pre>
  
  <p>Using the following test class...</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Test
  {
      public static void main(String args[]) throws Exception
      {
          Data data1 = new Data();
               data1.setName("Peter");
               data1.setAddress("Cologne");
  
          Data data2 = new Data();
               data2.setName("Mary");
               data2.setAddress("Hamburg");
  
          Database database = new Database();
                   database.getRecords().add(data1);
                   database.getRecords().add(data2);
  
          JAXBContext context = JAXBContext.newInstance(Database.class);
          Marshaller marshaller = context.createMarshaller();
                     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                     marshaller.marshal(database, new FileWriter("test.xml"));       
      }
  }
  </code></pre>
  
  <p>...I got the result:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;database&gt;
      &lt;records&gt;
          &lt;address&gt;Cologne&lt;/address&gt;
          &lt;name&gt;Peter&lt;/name&gt;
      &lt;/records&gt;
      &lt;records&gt;
          &lt;address&gt;Hamburg&lt;/address&gt;
          &lt;name&gt;Mary&lt;/name&gt;
      &lt;/records&gt;
  &lt;/database&gt;
  </code></pre>
  
  <p>But that's not what I was expecting, i.e. all tags for <code>&lt;Data&gt;</code> objects are missing. I am looking for a way to export the data in the following structure, but I don't know how to achieve this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;database&gt;
      &lt;records&gt;
          &lt;data&gt;
              &lt;address&gt;Cologne&lt;/address&gt;
              &lt;name&gt;Peter&lt;/name&gt;
          &lt;/data&gt;
          &lt;data&gt;
              &lt;address&gt;Hamburg&lt;/address&gt;
              &lt;name&gt;Mary&lt;/name&gt;
          &lt;/data&gt;
      &lt;/records&gt;
  &lt;/database&gt;
  </code></pre>
  
  <hr>
  
  <p>One additional question: if I want to deal with the problem <strong>without</strong> using <code>@XmlElementWrapper</code> and <code>@XmlElement</code> annotations, I can introduce an intermediary class</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Records
  {
      List&lt;Data&gt; data = new ArrayList&lt;Data&gt;();
  
      public List&lt;Data&gt; getData()                { return data; }
      public void       setData(List&lt;Data&gt; data) { this.data = data; }
  }
  </code></pre>
  
  <p>used by the modified base class</p>
  
  <pre class="lang-java prettyprint-override"><code>@XmlRootElement
  public class Database
  {
      Records records = new Records();
  
      public Records getRecords()                { return records; }
      public void    setRecords(Records records) { this.records = records; }
  }
  </code></pre>
  
  <p>in a slightly modified <code>Test</code> class:</p>
  
  <pre class="lang-java prettyprint-override"><code>...
  Database database = new Database();
  database.getRecords().getData().add(data1);
  database.getRecords().getData().add(data2);
  ...
  </code></pre>
  
  <p>The result also is:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;database&gt;
      &lt;records&gt;
          &lt;data&gt;
              &lt;address&gt;Cologne&lt;/address&gt;
              &lt;name&gt;Peter&lt;/name&gt;
          &lt;/data&gt;
          &lt;data&gt;
              &lt;address&gt;Hamburg&lt;/address&gt;
              &lt;name&gt;Mary&lt;/name&gt;
          &lt;/data&gt;
      &lt;/records&gt;
  &lt;/database&gt;
  </code></pre>
  
  <p>Is this the <strong>recommended</strong> way to create a Java class structure according to the XML file structure above?</p>