users@jaxb.java.net

Re: Inherited classes

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 21 Jul 2008 10:00:31 +0200

Hi.


> I start with JAXB and I fight with XSD structure.
>
> Consider following simplified XML structure (I can't change it):
> <?xml version="1.0" ?>
> <root>
> <foo>
> <x>1</x>
> <y>1</y>
> <q>string-value</q>
> </foo>
> <bar>
> <x>1</x>
> <y>1</y>
> <z>string-value</z>
> </bar>
> </root>
>
> I want to map same attributes to parent class like this:
> public class FooBar { //this class should be generated
> protected int x; //mapped x
> protected int y; //mapped y
> //..getters and setters for x & y
>
> }
>
> public class Foo extends FooBar {
> protected String q;
> //..getter and setter for q
> }
>
> public class Bar extends FooBar {
> protected String z;
> //..getter and setter for z
> }
>
> I can't figure out how XSD should like. Is it possible to map elements into
> classes like that? I use 6u10.

Use derivation by extension in your XSD.
Here's something to start with:

        <xs:element name="base" type="baseType"/>
        <xs:complexType name="baseType">
                <xs:sequence>
                        <xs:element name="a" type="xs:string"/>
                        <xs:element name="b" type="xs:long"/>
                </xs:sequence>
        </xs:complexType>
        <xs:element name="extended" type="extendedType"/>
        <xs:complexType name="extendedType">
                <xs:complexContent>
                    <xs:extension base="baseType">
                                <xs:sequence>
                                        <xs:element name="c" type="xs:dateTime"/>
                                        <xs:element name="d" type="xs:base64Binary"/>
                                </xs:sequence>
                    </xs:extension>
                </xs:complexContent>
        </xs:complexType>

Bye.
/lexi