title: Unmarshall xml list of objects to java list without class list
body: <p>With JAXB, I would like to unmarshall an xml document that contains several serialized objects like this :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Users&gt;
  &lt;User&gt;
      &lt;firstName&gt;first name value 1&lt;/firstName&gt;
      &lt;lastName&gt;last name value 1&lt;/lastName&gt;
      &lt;account&gt;
          &lt;expiration&gt;expire 1&lt;/expiration&gt;
          &lt;login&gt;login 1&lt;/login&gt;
      &lt;/account&gt;
  &lt;/User&gt;
  &lt;User&gt;
      &lt;firstName&gt;first name value 2&lt;/firstName&gt;
      &lt;lastName&gt;last name value 2&lt;/lastName&gt;
      &lt;account&gt;
          &lt;expiration&gt;expire 2&lt;/expiration&gt;
          &lt;login&gt;login 2&lt;/login&gt;
      &lt;/account&gt;
  &lt;/User&gt;
  ...
  &lt;/Users&gt;
  </code></pre>
  
  <p>The fact is that I don't want to create a new class named "Users" for instance and which contains a list of User's elements (with @XmlWrapper annotation).</p>
  
  <p>Here is my java object that I have to translate to xml :</p>
  
  <pre><code>@Entity
  @Table(name="USERS")
  @XmlRootElement(name="User")
  public class User implements Serializable {
  
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      private String firstName;
      private String lastName;
  
      @OneToOne(cascade={CascadeType.REMOVE}, mappedBy="user")
      private Account account;
  
      @XmlAttribute
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @XmlElement
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      @XmlElement
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      @XmlInverseReference(mappedBy="user")
      @XmlElement
      public Account getAccount() {
          return account;
      }
  
      public void setAccount(Account account) {
          this.account = account;
      }
  }
  </code></pre>
  
  <p>And for the moment I can juste unmarshall just one  xml to an User java object. Like this :</p>
  
  <pre><code>@Test
      public void test2() {
          try {
              JAXBContext jc = JAXBContext.newInstance(User.class);
              Unmarshaller u = jc.createUnmarshaller();
  
              File f = new File("user.xml");
              User element = (User) u.unmarshal(f);
  
              System.out.println(
                      element.getAccount().getLogin()
                      );
  
          } catch (JAXBException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>And I would like to get a User java list instances instead of just a User instance. Like this for instance :</p>
  
  <pre><code>List&lt;User&gt; elements = (List&lt;User&gt;) u.unmarshal(f);
  </code></pre>
  
  <p>I hope this is possible and I would like to know how ;)</p>
  
  <hr>
  
  <p>Thanks a lot for your reply Blaise.</p>
  
  <p>I tried to make as you did but I've got error :</p>
  
  <blockquote>
    <p>java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to com.thales.momoko.ws.model.User</p>
  </blockquote>
  
  <p>Here are some relevant part of my code :</p>
  
  <pre><code>public class Tools&lt;T&gt; {
  
  public List&lt;T&gt; getItems(Class&lt;T&gt; entityClass, String xmlLocation) {
      try {
          JAXBContext jc = JAXBContext.newInstance(Wrapper.class, entityClass.getClass());
          Unmarshaller unmarshaller = jc.createUnmarshaller();
  
          BufferedReader br = new BufferedReader(
                  new InputStreamReader(
                  this.getClass().getClassLoader().getResourceAsStream(xmlLocation)));
  
          System.out.println(br.readLine());
  
          Wrapper&lt;T&gt; wrapper = (Wrapper&lt;T&gt;) unmarshaller.unmarshal(new StreamSource(br), Wrapper.class).getValue();
  
          System.out.println(wrapper);
  
          return wrapper.getItems();
  
      } catch (JAXBException ex) {
          Logger.getLogger(Tools.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
          Logger.getLogger(Tools.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
  }
  }
  </code></pre>
  
  <p>The first println works fine because it displays the first line of the xml file :</p>
  
  <pre><code>&lt;?xml version=....&gt;
  </code></pre>
  
  <p>The second println shows a problem with the unmarshalling :</p>
  
  <blockquote>
    <p>Wrapper{items=[[user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null], [user: null]]}</p>
  </blockquote>
  
  <p>The wrapper :</p>
  
  <pre><code>public class Wrapper&lt;T&gt; {
  
  private List&lt;T&gt; items = new ArrayList&lt;&gt;();
  
  @XmlAnyElement(lax=true)
  public List&lt;T&gt; getItems() {
      return items;
  }
  
  @Override
  public String toString() {
      return "Wrapper{" + "items=" + items + '}';
  }
  }
  </code></pre>
  
  <p>And finally the call of the unmarshaller :</p>
  
  <pre><code>@PostConstruct
  public void init() {
     this.entityClass = User.class;
      for (User user : (List&lt;User&gt;) new Tools&lt;User&gt;().getItems(User.class, "user.xml"))
          System.out.println(user.getFirstName());
  }
  </code></pre>
  
  <p>It gives me an error at the line with the "for" instruction.</p>
  
  <p>Have you got an idea about this error ?</p>
  
  <p>Thanks another time !</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Solution :</p>
  
  <pre><code>public class Tools&lt;T&gt; {
  
  public static &lt;T&gt; List&lt;T&gt; getItems(Class&lt;T&gt; entityClass, String xmlLocation) {
      try {
          JAXBContext jc;
          synchronized (JAXBContext.class) {
              jc = JAXBContext.newInstance(Wrapper.class, entityClass);
          }
  
          Unmarshaller unmarshaller = jc.createUnmarshaller();
          BufferedReader br = new BufferedReader(
                  new InputStreamReader(
                  Import.class.getClassLoader().getResourceAsStream(xmlLocation)));
  
          Wrapper&lt;T&gt; wrapper = (Wrapper&lt;T&gt;) unmarshaller.unmarshal(new StreamSource(br), Wrapper.class).getValue();
  
          return wrapper.getItems();
  
      } catch (JAXBException ex) {
          Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
  }
  }
  </code></pre>
  
  <p>The call :</p>
  
  <pre><code>@PostConstruct
  public void init() {
     this.entityClass = User.class;
      for (User user : (List&lt;User&gt;) Tools.getItems(User.class, "user.xml"))
          em.persist(user);
  }
  </code></pre>