title: JAXB doesn't unmarshal list of interfaces
body: <p>It seems JAXB can't read what it writes. Consider the following code:</p>
  
  <pre><code>interface IFoo {
      void jump();
  }
  
  @XmlRootElement
  class Bar implements IFoo {
      @XmlElement
      public String y;
  
      public Bar() {
          y = "";
      }
  
      public Bar(String y) {
          this.y = y;
      }
  
      @Override
      public void jump() {
          System.out.println(y);
      }
  }
  
  @XmlRootElement
  class Baz implements IFoo {
      @XmlElement
      public int x;
  
      public Baz() {
          x = 0;
      }
  
      public Baz(int x) {
          this.x = x;
      }
  
      @Override
      public void jump() {
          System.out.println(x);
      }
  }
  
  @XmlRootElement
  public class Holder {
      private List&lt;IFoo&gt; things;
  
      public Holder() {
          things = new ArrayList&lt;&gt;();
      }
  
      @XmlElementWrapper
      @XmlAnyElement
      public List&lt;IFoo&gt; getThings() {
          return things;
      }
  
      public void addThing(IFoo thing) {
          things.add(thing);
      }
  }
  
  // ...
  
  try {
      JAXBContext context = JAXBContext.newInstance(Holder.class, Bar.class, Baz.class);
  
      Holder holder = new Holder();
      holder.addThing(new Bar("1"));
      holder.addThing(new Baz(2));
      holder.addThing(new Baz(3));
  
      for (IFoo thing : holder.getThings()) {
          thing.jump();
      }
  
      StringWriter s = new StringWriter();
      context.createMarshaller().marshal(holder, s);
  
      String data = s.toString();
  
      System.out.println(data);
  
      StringReader t = new StringReader(data);
      Holder holder2 = (Holder)context.createUnmarshaller().unmarshal(t);
  
      for (IFoo thing : holder2.getThings()) {
          thing.jump();
      }
  }
  catch (Exception e) {
      System.err.println(e.getMessage());
  }
  </code></pre>
  
  <p>It's a simplified example, of course. The point is that I have to store two very differently implemented classes, Bar and Baz, in one collection. Well, I observed that they have pretty similar public interface, so I created an interface IFoo and made them two to implement it. Now, I want to have tools to save and load this collection to/from XML. Unfortunately, this code doesn't quite work: the collection is saved, but then it cannot be loaded! The intended output is </p>
  
  <pre><code>1
  2
  3
  some xml
  1
  2
  3
  </code></pre>
  
  <p>But unfortunately, the actual output is</p>
  
  <pre><code>1
  2
  3
  some xml
  com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to testapplication1.IFoo
  </code></pre>
  
  <p>Apparently, I need to use the annotations in a different way? Or to give up on JAXB and look for something else? I, well, can write "XMLNode toXML()" method for all classes I wan't to (de)marshal, but...</p>