4

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?

1 Answer 1

11

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:

@XmlElements({
    @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.

Sign up to request clarification or add additional context in comments.

1 Comment

Eventually combine with: @XmlElementWrapper(name="children")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.