title: Marshalling a List of objects implementing a common interface, with JaxB
body: <p>I am trying to marshall a list of objects implementing a common interface.
  There are 3 classes and 1 interface involved:</p>
  
  <p><strong>Community</strong> class (has one method: <strong>List&lt;Person&gt; getPeople();</strong>)</p>
  
  <p><strong>Person</strong> interface (has one method: <strong>String getName();</strong>)</p>
  
  <p><strong>Girl</strong> class (implements Person)</p>
  
  <p><strong>Boy</strong> class (implements Person)</p>
  
  <p>See code below.</p>
  
  <p>I want an XML that looks something like this:</p>
  
  <pre><code>&lt;community&gt;
    &lt;people&gt;
      &lt;girl&gt;
        &lt;name&gt;Jane&lt;/name&gt;
      &lt;/girl&gt;
      &lt;boy&gt;
        &lt;name&gt;John&lt;/name&gt;
      &lt;/boy&gt;
      &lt;girl&gt;
        &lt;name&gt;Jane&lt;/name&gt;
      &lt;/girl&gt;
      &lt;boy&gt;
        &lt;name&gt;John&lt;/name&gt;
      &lt;/boy&gt;
    &lt;/people&gt;
  &lt;/community&gt;
  </code></pre>
  
  <p>or possibly:</p>
  
  <pre><code>&lt;community&gt;
    &lt;people&gt;
     &lt;person&gt;
        &lt;girl&gt;
          &lt;name&gt;Jane&lt;/name&gt;
        &lt;/girl&gt;
      &lt;/person&gt;
      &lt;person&gt;
        &lt;boy&gt;
          &lt;name&gt;John&lt;/name&gt;
        &lt;/boy&gt;
      &lt;/person&gt;
    &lt;/people&gt;
  &lt;/community&gt;
  </code></pre>
  
  <p>So far what I get is this:</p>
  
  <pre><code>&lt;community&gt;
      &lt;people&gt;
          &lt;person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="girl"&gt;
              &lt;name&gt;Jane&lt;/name&gt;
          &lt;/person&gt;
          &lt;person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="boy"&gt;
              &lt;name&gt;John&lt;/name&gt;
          &lt;/person&gt;
          &lt;person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="girl"&gt;
              &lt;name&gt;Jane&lt;/name&gt;
          &lt;/person&gt;
          &lt;person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="boy"&gt;
              &lt;name&gt;John&lt;/name&gt;
          &lt;/person&gt;
      &lt;/people&gt;
  &lt;/community&gt;
  </code></pre>
  
  <p>I realize I can change the  element to something else, but I want the element name to be the name spesified in the Girl or Boy class.</p>
  
  <p>Can this be done? Thanks.</p>
  
  <pre><code>@XmlRootElement(name = "community")
  public class Community {
  
   private List&lt;Person&gt; people;
  
   @XmlElementWrapper
   @XmlElement(name="person")
   public List&lt;Person&gt; getPeople() {
    return people;
   }
  
   public Community() {
    people = new ArrayList&lt;Person&gt;();
    people.add(new Girl());
    people.add(new Boy());
    people.add(new Girl());
    people.add(new Boy());
   }
  }
  
  
  
  
  
  
  
  @XmlRootElement(name = "girl")
  public class Girl implements Person {
  
   @XmlElement
   public String getName() {
    return "Jane";
   }
  }
  
  
  @XmlRootElement(name = "boy")
  public class Boy implements Person {
  
   @XmlElement
   public String getName() {
    return "John";
   }
  }
  
  
  
  @XmlJavaTypeAdapter(AnyTypeAdapter.class)
  public interface Person {
   public String getName();
  }
  
  
  
  public class AnyTypeAdapter extends XmlAdapter&lt;Object, Object&gt; {
  
   @Override
     public Object marshal(Object v) throws Exception {
      return v;
     }
  
   @Override
     public Object unmarshal(Object v) throws Exception {
      return v;
     }
  
  }
  </code></pre>