Java JAXB and inheritance


I want to create a list different types of class, which are all inheritance from the same superClass.
I am starting from an xsd file and I want to create the java classes and in run time load xml file that has a list nodes.
My problem is define the xsd that will create the java classes.

I am using JAXB 2.0 eclipse plugin.

In the end I want to have one List<superClass>.
Can I do it with a simple JAXB?

Best Solution

I don't understand why you want to start with the xsd if you don't already have it. If you're free about the schema it'd start with the java code and generate the XSD from there.

You can annotate a list as follows:

    @XmlElement(name = "child1", type = Child1.class),
    @XmlElement(name = "child2", type = Child2.class),
    @XmlElement(name = "child3", type = Child3.class)})
private final List<IChild> children = new ArrayList<IChild>();

Where IChild is the Interface for subclasses of superClass. This will generate a XSD-Schema as you described teh on you wanted.