title: Parse XML to specific subclass according to tag
body: <p>Suppose I have these classes:</p>
  
  <pre><code>public class Container {
      private String name;
      private int number;
      private List&lt;ParentClass&gt; list;
  }
  
  public class ParentClass {
      private String value;
  }
  </code></pre>
  
  <p>If I were to read it from a XML file using JAXB, I'd expect the XML to be something like this:</p>
  
  <pre><code>&lt;container&gt;
      &lt;name&gt;Name&lt;/name&gt;
      &lt;number&gt;1&lt;/number&gt;
      &lt;subclass&gt;
          &lt;value&gt;Val&lt;/value&gt;
      &lt;/subclass&gt;
      &lt;subclass&gt;
          &lt;value&gt;Val2&lt;/value&gt;
      &lt;/subclass&gt;
      &lt;subclass&gt;
          &lt;value&gt;Val3&lt;/value&gt;
      &lt;/subclass&gt;
  &lt;/container&gt;
  </code></pre>
  
  <p>And I'd decorate my classes like this:</p>
  
  <pre><code>@XmlRootElement(name = "container")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Container {
      @XmlElement(name = "name")
      private String name;
  
      @XmlElement(name = "number")
      private int number;
  
      @XmlElement(name = "subclass", type = Subclass.class)
      private List&lt;ParentClass&gt; list;
  }
  
  @XmlRootElement(name = "container")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class ParentClass {
      @XmlElement(name = "value")
      private String value;
  }
  </code></pre>
  
  <p>Now, suppose <code>ParentClass</code> was actually an abstract class or an interface, and I had two subclasses that derived from it; something like:</p>
  
  <pre><code>public abstract ParentClass {
      void doSomething();
  }
  
  public IntSubclass extends ParentClass {
      private int value;
      public void doSomething() { ... }
  }
  
  public StringSubclass extends ParentClass {
      private String value;
      public void doSomething() { ... }
  }
  </code></pre>
  
  <p>How can I use JAXB and XML to be able to parse the <code>list</code> attribute to be a list of <code>StringSubclass</code> or <code>IntSubclass</code> accordingly? </p>
  
  <p>That is, if I were to read <code>StringSubclass</code> elements or <code>IntSubclass</code> elements from the XML and be able to recognize they are one kind or the other. Something like this:</p>
  
  <pre><code>&lt;container&gt;
      &lt;name&gt;Name&lt;/name&gt;
      &lt;number&gt;1&lt;/number&gt;
      &lt;stringSubclass&gt;
          &lt;value&gt;Val&lt;/value&gt;
      &lt;/stringSubclass&gt;
      &lt;stringSubclass&gt;
          &lt;value&gt;Val2&lt;/value&gt;
      &lt;/stringSubclass&gt;
      &lt;intSubclass&gt;
          &lt;value&gt;3&lt;/value&gt;
      &lt;/intSubclass&gt;
  &lt;/container&gt;
  </code></pre>
  
  <p>Is there any way? Maybe with another framework?</p>
  
  <p>Thanks</p>