title: JAXB - XmlElement with multiple names and types
body: <p>I have the following class hierarchy:</p>
  
  <pre><code>@XmlRootElement
  public abstract class Animal{}
  
  @XmlRootElement
  public class Dog extends Animal{}
  
  @XmlRootElement
  public class Cat extends Animal{}
  
  @XmlRootElement
  public class Lion extends Animal{}
  </code></pre>
  
  <p>and a class which has an attribute named animal:</p>
  
  <pre><code>@XmlRootElement
  public class Owner{
    private Animal animal;
  }
  </code></pre>
  
  <p>I would like to allow different XML Schemas as follows and bind the Animal Type in the schema to <code>animal object</code> in <code>Owner class</code></p>
  
  <pre><code>&lt;Owner&gt;
   &lt;Dog&gt;&lt;/Dog&gt;
  &lt;/Owner&gt;
  
  &lt;Owner&gt;
   &lt;Cat&gt;&lt;/Cat&gt;
  &lt;/Owner&gt;
  
  &lt;Owner&gt;
   &lt;Lion&gt;&lt;/Lion&gt;
  &lt;/Owner&gt;
  </code></pre>
  
  <p>The solutions that I have found use <code>XmlElements</code> which can take multiple <code>XmlElement</code> fields and creates a collection. However, in my case I don't need a collection but a single attribute.</p>
  
  <p>Does JAXB allow any XmlElement multiple naming convention for this problem?
  Is there any other annotation which could solve this problem?</p>
  
  <p>Note: I have looked at multiple answers to similar questions in stackoverflow and around but almost all of them create a collection instead of a single object. The closest answer I have found is this : <a href="https://stackoverflow.com/questions/24554789/xmlelement-with-multiple-names">@XmlElement with multiple names</a></p>
  
  <p>Edit : I think <a href="http://blog.bdoughan.com/2010/10/jaxb-and-xsd-choice-xmlelements.html" rel="nofollow noreferrer">this</a> solution might work. Have to test it out</p>