ktm5124
ktm5124

Reputation: 12123

Why does JAXB handle extensions differently?

Consider the following XSD file:

<xs:element name="person" type="Person"/>
<xs:element name="teacher" type="Teacher"/>

<xs:complexType name="Person">
    <xs:sequence>
       <xs:element name="age" type="xs:int"/>
       <xs:element name="sex" type="xs:string"/>
       <xs:element name="fullname" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="Teacher">
    <xs:complexContent>
        <xs:extension base="Person">
            <xs:sequence>
                <xs:element name="school" type="xs:string"/>
                <xs:element name="grade" type="xs:string"/>
                <xs:element name="subject" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

I used JAXB to generate Java classes for these complex types. The strange thing is, the Teacher class was annotated with @XmlRootElement, whereas the Person class was not.

Does anyone know why this is?

Upvotes: 1

Views: 604

Answers (1)

bdoughan
bdoughan

Reputation: 149047

Top level elements with anonymous complex types get @XmlRootElement annotations generated on them. Top level elements with a named complex type have @XmlElementDecl annotations on the generated ObjectFactory. Teacher should not have been generated with an @XmlRootElement unless some sort of binding customization is used.

For More Information

I have writtent more about this on my blog:

Upvotes: 1

Related Questions