title: Marshall a List to XML works - but how to unmarshall?
body: <p>I can marshall a ObservableList using a "Wrapper"-class like below. But I cannot unmarshall it back to the wrapperclass it was before.</p>
  
  <p>The idea is:
  I have an ObservableList of "Expenses". I put this List into a wrapper-class and save this class to XML. The result looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;List&gt;
      &lt;root&gt;
          &lt;category&gt;[none]&lt;/category&gt;
          &lt;period&gt;Year&lt;/period&gt;
          &lt;title&gt;asd&lt;/title&gt;
          &lt;value&gt;354&lt;/value&gt;
      &lt;/root&gt;
  &lt;/List&gt;
  </code></pre>
  
  <p>I cannot bring it back to the wrapper-object.
  I really appreciate any kind of help.</p>
  
  <p><strong>Main-class JAXBContext (visible for all):</strong></p>
  
  <pre><code>JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class, Expense.class);
  </code></pre>
  
  <p><strong>Main-class SAVEBUTTON:</strong></p>
  
  <pre><code>public class SaveButtonListener implements EventHandler&lt;ActionEvent&gt; {
  
      @Override
      public void handle(ActionEvent arg0) {
  
          File serializedFile = new File(PATH);
  
          try {
              if (serializedFile.exists() == false)
              serializedFile.createNewFile();
  
              PrintWriter xmlOut = new PrintWriter(serializedFile);
  
              Marshaller m = jc.createMarshaller();
              m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
              List&lt;Expense&gt; saveList = new ArrayList&lt;&gt;();
  
              saveList.addAll(data);
  
  
  
              MyWrapperForList&lt;Expense&gt; wrapper = new MyWrapperForList&lt;&gt;(saveList);
                  JAXBElement&lt;MyWrapperForList&gt; jaxbElement = new JAXBElement&lt;&gt;(
  new QName("List"), MyWrapperForList.class, wrapper);
  
  
  
          m.marshal(jaxbElement, xmlOut);
  
              xmlOut.flush();
              xmlOut.close();
  </code></pre>
  
  <p><strong>Main-class-LOADBUTTON:</strong></p>
  
  <pre><code>public class LoadButtonListener implements EventHandler&lt;ActionEvent&gt; {
  
      @Override
      public void handle(ActionEvent arg0) {
  
  
          try {
              Unmarshaller unmarshaller = jc.createUnmarshaller();
  
  
  
  
  StreamSource xml = new StreamSource(PATH);
                  MyWrapperForList&lt;Expense&gt; unwrapper = unmarshaller.unmarshal(xml,
   MyWrapperForList.class).getValue();
  
              List&lt;Expense&gt; tempList = new ArrayList&lt;&gt;();
              tempList.addAll(unwrapper.getItems());
  
  
              System.out.println(tempList.get(0).getTitle());
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p><strong>Wrapper-class:</strong></p>
  
  <p>public class MyWrapperForList {</p>
  
  <pre><code>private List&lt;Expense&gt; list;
  
  public MyWrapperForList() {
      list = new ArrayList&lt;&gt;();
  }
  
  public MyWrapperForList(List&lt;Expense&gt; expenses) {
      this.list = expenses;
  }
  
  @XmlAnyElement(lax=true)
  public List&lt;Expense&gt; getItems() {
      return list;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Expense-class:</strong></p>
  
  <p>@XmlRootElement(name = "root")
  public class Expense {</p>
  
  <pre><code>private String title;
  private String category;
  private String period;
  private String value;
  
  public Expense() {} //Default constructor is needed for XML-handling
  
  public Expense(String title, String value, String period, String category) {
      this.title = title;
      this.value = value;
      this.period = period;
      this.category = category;
  }
  
  @XmlElement(name = "title")
  public String getTitle() {
      return this.title;
  }
  
  @XmlElement(name = "category")
  public String getCategory() {
      return this.category;
  }
  
  @XmlElement(name = "period")
  public String getPeriod() {
      return this.period;
  }
  
  @XmlElement(name = "value")
  public String getValue() {
      return this.value;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I used this tutorial from Blaise Doughan: <a href="http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html" rel="nofollow">http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html</a></p>