users@jaxb.java.net

Re: Default value for ComplexType

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Tue, 13 Nov 2007 10:36:47 +0100

Hi.

> If I've such type in my schema
>
> <xs:complexType name="pageColor">
> <xs:sequence>
> <xs:element name="background" type="xs:string" default="red"/>
> <xs:element name="foreground" type="xs:string" default="blue"/>
> </xs:sequence>
> </xs:complexType>
>
> <xs:complexType name="foo">
> <xs:sequence>
> <xs:element name="pageColor" type="tns:pageColor" minOccurs="1"
> maxOccurs="1"/>
> </xs:sequence>
> </xs:complexType>
>
>
> Is it possible to have a default instance of PageColor in the Foo, So that
>
> foo.getPageColor();
>
> will not return NULL?

Take a look at this:

https://jaxb.dev.java.net/guide/Adding_behaviors.html

You could override the ObjectFactory so that it creates instances with
default values. Or you could insert your own FooEx (with initialized
pageColor) into the class hierarchy.

Bye.
/lexi