title: JAXB 2.x: How to unmarshal an XML without knowing the target class?
body: <p>If there is a way, how to do this, I'd like to know the most elegant one.
  Here is the question:
  - Let's assume you have an abstract class Z
  - You have two classes inherited from Z: named A and B.</p>
  
  <p>You marshal any instance (A or B) like this:</p>
  
  <pre><code>JAXBContext context = JAXBContext.newInstance(Z.class);
  Marshaller m = context.createMarshaller();
  m.marshal(jaxbObject, ...an outputstream...);
  </code></pre>
  
  <p>In the resulting XML you see what kind of instance it was (A or B).</p>
  
  <p>Now, how do you unmarshall like</p>
  
  <pre><code>JAXBContext jc = JAXBContext.newInstance(Z.class);
  Unmarshaller u = jc.createUnmarshaller();
  u.unmarshal(...an inputstream...)
  </code></pre>
  
  <p>I get an UnmarshalException saying</p>
  
  <blockquote>
  <pre><code>"Exception Description: A descriptor with default root element {&lt;my namespace&gt;}&lt;the root tag, e.g. A or B&gt; was not found in the project]
  </code></pre>
    
    <p>javax.xml.bind.UnmarshalException"</p>
  </blockquote>
  
  <p>So how do you do unmarshalling so that you get an instance of Z and then you can test AFTER unmarshalling, what it is? e.g. z instanceof A then... z instanceof B then something else... etc.</p>
  
  <p>Thanks for any ideas or solutions.</p>
  
  <p>I am using JRE1.6 with MOXy as JAXB Impl.</p>