title: Multiple XML elements to the same class
body: <p>I'd like to do the following with JAXB, but don't know, how to. I'll show you some code, first, and after that, the details.</p>
  
  <pre><code>@XmlRootElement(name = "root")
  public class RootClass {    
      @XmlElementWrapper("foos")
      // ?? - I do not want to list here all my xml element 
      // types with @XmlElements({...})
      private List&lt;Foo&gt; fooList;    
  }
  
  public class Foo {    
      @XmlTransient
      // how to map the element's type (based on the xml element)
      // into this attribute?
      private FooType type;
  
      @XmlElement(name = "something", type = Something.class)
      private Something something;    
  }
  
  public class Baz extends Foo {
      @XmlElement(name = "somethingMore", type = SomethingMore.class)
      private SomethingMore more;
  }
  
  public enum FooType {
      BAR,
      QUX
  }
  </code></pre>
  
  <p>And the XML structure:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root xmlns="mynamespace"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="myschemalocation"&gt;
  
      &lt;foos&gt;
          &lt;bar&gt;
              &lt;something ... /&gt;
          &lt;/bar&gt;
          &lt;qux&gt;
              &lt;something ... /&gt;
          &lt;/qux&gt;
          &lt;baz&gt;
              &lt;something ... /&gt;
              &lt;somethingMore ... /&gt;
          &lt;/baz&gt;
      &lt;/foos&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>So, I have the classes above, and the XML structure. Almost all my Foo-s are almost the same, but they differ in the FooType attribute. But some of my Foo-s are very-very different: I'd like to create a new class (which extends the original Foo) for them. In this example this class called <code>Baz</code>.</p>
  
  <p>Is this possible, with JAXB, to unmarshall this structure? At the end of this example, I'd like to get a RootClass object, which has a list with 3 elements:</p>
  
  <ul>
  <li>one Foo object, which's type is <code>BAR</code></li>
  <li>one Foo object, which's type is <code>QUX</code></li>
  <li>and one <code>Baz</code> object.</li>
  </ul>
  
  <p>Is it possible, with JAXB somehow? I've searched for hours. I've tried to create an 'ObjectFactory' with the <code>@XmlRegistry</code> and <code>@XmlElementDecl</code> annotations, but I'm not sure, how to do this.</p>