title: Is it possible to have same element name for multiple classes in JAXB?
body: <p>I am working with JAXB marshal and unmarshal . I have following scenario where I have to use same name(Person.ELEMENT_NAME) for 2 <code>@XmlRootElement</code> . </p>
  
  <pre><code>@XmlRootElement
  @XmlAccessorType(XmlAccessType.NONE)
  public class Person{
  
      public final static String ELEMENT_NAME = "person";
  
      @XmlElement
      private String name;
      @XmlElement
      private String title;
      //Getter Setter
  }
  
  @XmlRootElement(name = Person.ELEMENT_NAME)
  public class ReceiverPerson extends Person {
      @XmlElement
      private String gender;
      //Getter Setter 
  }
  
  @XmlRootElement(name = Person.ELEMENT_NAME)
  public class SenderPerson extends Person {
      @XmlElement
      private String age;
      //Getter Setter 
  }
  @XmlRootElement
  @XmlAccessorType(XmlAccessType.NONE)
  public class Human {
  
      @XmlElement(name = Person.ELEMENT_NAME , type = ReceiverPerson.class)
      private ReceiverPerson rP;
      @XmlElement(name = Person.ELEMENT_NAME , type= SenderPerson.class)
      private SenderPerson sP;
      @XmlElement
      private String type;
  
      @XmlAnyElement
      private List&lt;Object&gt; unknown;
  
       //Getter Setter
  }
  
  public class Test{
   public static void main(String[] args){
  
          StringWriter sw = new StringWriter();
      JAXBContext jc = JAXBContext.newInstance(Human.class);
      Marshaller marshaller = jc.createMarshaller();
      Unmarshaller unmarshaller = jc.createUnmarshaller();
      Human human2,human1;
      String humans = "This xml is writtem after my code , because its too big";
      human1 = new Human();
      human1.setType("Human");
      SenderPerson sp = new SenderPerson();
      sp.setAge("23");
      sp.setName("Marry");
      sp.setTitle("Lecturer");
      ReceiverPerson rP = new ReceiverPerson();
      rP.setName("John");
      rP.setTitle("Professor");
      rP.setGender("Male");
  
  
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.marshal(human1, sw);
      System.out.println("Marshalling by creating a Human object");
      System.out.println(sw.toString());
  
  
  
      human2 = (Human) unmarshaller.unmarshal(new StringReader(sw.toString()));
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.marshal(human2, sw);
      System.out.println("Marshalling from my own xml");
      System.out.println(sw.toString());
  
  }
  }
  </code></pre>
  
  <p>The String humans in Test class has following xml:</p>
  
  <pre><code>&lt;human&gt;
  &lt;type&gt;Human Being&lt;/type&gt;
  &lt;person type="ReceiverPerson"&gt;
  &lt;name&gt;John&lt;/name&gt;
  &lt;title&gt;Professor&lt;/title&gt;
  &lt;gender&gt;Male&lt;/gender&gt;
  &lt;/person&gt;
  &lt;person type="SenderPerson"&gt;
  &lt;name&gt;Marry&lt;/name&gt;
  &lt;title&gt;Lecturer&lt;/title&gt;
  &lt;age&gt;23&lt;/age&gt;
  &lt;/person&gt;
  &lt;anything&gt;I am unknown to the xml&lt;/anything&gt;
  &lt;/human&gt;
  </code></pre>
  
  <p>I don't know what's going on but i get following output :</p>
  
  <pre><code>Marshalling by creating a Human object
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;human&gt;
      &lt;type&gt;Human&lt;/type&gt;
  &lt;/human&gt;
  
  Marshalling from my own xml
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;human&gt;
      &lt;type&gt;Human&lt;/type&gt;
  &lt;/human&gt;
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;human&gt;
      &lt;person&gt;
          &lt;name&gt;Marry&lt;/name&gt;
          &lt;title&gt;Lecturer&lt;/title&gt;
      &lt;/person&gt;
      &lt;type&gt;Human Being&lt;/type&gt;
      &lt;anything&gt;I am unknown to the xml&lt;/anything&gt;
  &lt;/human&gt;
  </code></pre>
  
  <p>Sorry for the big examples , my questions are as follows </p>
  
  <ol>
  <li><p>Why is the first output missing the persons ?</p></li>
  <li><p>Why is the second output missing the <code>age</code> ?</p></li>
  <li><p>Why is the second output missing the whole person for ReceiverElement (The description for John)?</p></li>
  <li><p>Why is there two <code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;</code> in my second output ?</p></li>
  <li><p>Is it possible to have something like <code>&lt;person type="ReceiverPerson"&gt;</code> in the xml's by using the <code>@XmlElement(name = Person.ELEMENT_NAME , type = ReceiverPerson.class)</code> or in any other way ?</p></li>
  <li><p>The output of the last paragraph (unmarshall the produced xml and then marshal) does not give the proper output , the output is like :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;human&gt;
  &lt;person&gt;
      &lt;name&gt;Marry&lt;/name&gt;
      &lt;title&gt;Lecturer&lt;/title&gt;
  &lt;/person&gt;
  &lt;type&gt;Human&lt;/type&gt;
  &lt;/human&gt;
  </code></pre></li>
  </ol>
  
  <p>For some reason the Person subclasses are not being generated . Any idea please ?</p>
  
  <p>Thanks in advance for anyone who has atleast read this lengthy question </p>