Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Are you looking to use the path attribute as the inheritance indicator? If so the following will help:</p> <ul> <li><strong>Stack Overflow:</strong> <a href="https://stackoverflow.com/questions/2992234/java-jaxb-unmarshall-xml-to-specific-subclass-based-on-an-attribute/3181871#3181871">Java/JAXB: Unmarshall Xml to specific subclass based on an attribute</a></li> <li><strong>Blog Post:</strong> <a href="http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-moxy-extension.html" rel="nofollow noreferrer">http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-moxy-extension.html</a></li> </ul> <hr/> <p>I'm still not 100% sure I understand your use case but what about the following:</p> <p><strong>Stereotypes</strong></p> <pre><code>import java.util.List; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Stereotypes { private List&lt;AbstractStereotype&gt; sterotypes; @XmlElementRef public List&lt;AbstractStereotype&gt; getSterotypes() { return sterotypes; } public void setSterotypes(List&lt;AbstractStereotype&gt; sterotypes) { this.sterotypes = sterotypes; } } </code></pre> <p><strong>AbstractStereotype</strong></p> <pre><code>import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlSeeAlso; @XmlSeeAlso({Stereotype1.class, Stereotype2.class}) public abstract class AbstractStereotype { @XmlAttribute public abstract String getPath(); } </code></pre> <p><strong>Stereotype1</strong></p> <pre><code>import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Stereotype1 extends AbstractStereotype { public String getPath() { return "Path1"; } } </code></pre> <p><strong>Stereotype2</strong></p> <pre><code>import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Stereotype2 extends AbstractStereotype { public String getPath() { return "Path2"; } } </code></pre> <p><strong>Demo</strong></p> <pre><code>import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Stereotypes.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); Stereotypes stereotypes = (Stereotypes) unmarshaller.unmarshal(new File("input.xml")); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(stereotypes, System.out); } } </code></pre> <p><strong>input.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;stereotypes&gt; &lt;stereotype1/&gt; &lt;stereotype2/&gt; &lt;/stereotypes&gt; </code></pre> <p><strong>Output</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;stereotypes&gt; &lt;stereotype1 path="Path1"/&gt; &lt;stereotype2 path="Path2"/&gt; &lt;/stereotypes&gt; </code></pre> <p><strong>For More Information</strong></p> <ul> <li><a href="http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html" rel="nofollow noreferrer">http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload