Rectangle 27 17

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="todo">
        <xs:sequence>
            <xs:choice maxOccurs="unbounded">
                <xs:element name="doLaundry" type="doLaundry" />
                <xs:element name="washCar" type="washCar" />
                <xs:element name="tidyBedroom" type="tidyBedroom" />
            </xs:choice>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType abstract="true" name="Task">
        <xs:attribute name="cost" type="xs:int" use="required" />
    </xs:complexType>

    <xs:complexType name="doLaundry">
        <xs:complexContent>
            <xs:extension base="Task">
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="washCar">
        <xs:complexContent>
            <xs:extension base="Task">
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="tidyBedroom">
        <xs:complexContent>
            <xs:extension base="Task">
                <xs:attribute name="experiencePoints" type="xs:int" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>

combined with a binding file:

<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings>
      <jxb:bindings schemaLocation="todo.xsd" node="/xs:schema/xs:complexType[@name='todo']/xs:sequence/xs:choice">
            <jxb:property name="Tasks"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

Will give abstract and inherited classes as I described in the question. The binding file will change Jaxb's default method name from getDoLaundryOrWashCarOrTidyBedroom() to getTasks().

Polymorphism in XSD schema and JAXB classes - Stack Overflow

xsd jaxb
Rectangle 27 6

xsd:choice corresponds to the @XmlElements annotation. You could apply this annotation directly to your desired object model.

I think I didn't make my question clear enough intially. I am starting with an XSD schema and want to generate the Java classes from that with JAXB. I'd like to know if it is possible to structure the XSD in such a way as to give the desired Java object model - not the other way round.

You could define an inheritance relationship between the types in your XML schema. Check out this post for an example: bdoughan.blogspot.com/2010/11/

Polymorphism in XSD schema and JAXB classes - Stack Overflow

xsd jaxb
Rectangle 27 0

Maybe I'm not 'getting' the question, but what is wrong with..

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="todo">
        <xs:sequence>
            <xs:choice maxOccurs="unbounded">
                <xs:element name="doLaundry" type="task" />
                <xs:element name="washCar" type="task" />
                <xs:element name="tidyBedroom" type="task" />
            </xs:choice>
        </xs:sequence>
    </xs:complexType>


    <xs:complexType name="task">
        <xs:attribute name="cost" type="xs:int" />
        <xs:attribute name="experiencePoints" type="xs:int" />
    </xs:complexType>
</xs:schema>

Given this schema, Jaxb will not generate the class hierarchy I showed in the question. It will create a single class 'Task' and after unmarshalling the xml, there would be no way to distinguish one instance of Task from another. For example, apart from their cost, there would be no difference between the doLaundry and washCar Task objects.

Polymorphism in XSD schema and JAXB classes - Stack Overflow

xsd jaxb
Rectangle 27 0

Maybe I'm not 'getting' the question, but what is wrong with..

Given this schema, Jaxb will not generate the class hierarchy I showed in the question. It will create a single class 'Task' and after unmarshalling the xml, there would be no way to distinguish one instance of Task from another. For example, apart from their cost, there would be no difference between the doLaundry and washCar Task objects.

Polymorphism in XSD schema and JAXB classes - Stack Overflow

xsd jaxb
Rectangle 27 0

xsd:choice corresponds to the @XmlElements annotation. You could apply this annotation directly to your desired object model.

I think I didn't make my question clear enough intially. I am starting with an XSD schema and want to generate the Java classes from that with JAXB. I'd like to know if it is possible to structure the XSD in such a way as to give the desired Java object model - not the other way round.

You could define an inheritance relationship between the types in your XML schema. Check out this post for an example: bdoughan.blogspot.com/2010/11/

Polymorphism in XSD schema and JAXB classes - Stack Overflow

xsd jaxb
Rectangle 27 0

combined with a binding file:

Will give abstract and inherited classes as I described in the question. The binding file will change Jaxb's default method name from getDoLaundryOrWashCarOrTidyBedroom() to getTasks().

Polymorphism in XSD schema and JAXB classes - Stack Overflow

xsd jaxb
Rectangle 27 0

Use xs:extension in your schema and your JAXB classes will be inherited (extended) as you define in your schema.

Polymorphism in XSD schema and JAXB classes - Stack Overflow

xsd jaxb
Rectangle 27 0

Use xs:extension in your schema and your JAXB classes will be inherited (extended) as you define in your schema.

Polymorphism in XSD schema and JAXB classes - Stack Overflow

xsd jaxb