users@jaxb.java.net

Re: How to make a list mapping manditory

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Fri, 13 Oct 2006 16:32:01 -0700

Tim Kettler wrote:
> Hi,
>
> I have a set of existing java classes that should be persisted to XML. So far the mapping
> is working as expected. The only thing I failed to figure out by myself is how to specify
> that a list property like the one below should be manditory.
>
> @XmlElements({
> @XmlElement( name="stringProperty", type=StringProperty.class ),
> @XmlElement( name="integerProperty", type=IntegerProperty.class ),
> @XmlElement( name="containerProperty", type=ContainerProperty.class )
> })
> @XmlElementWrapper( name="containedProperties" )
> @Override
> public Set<Property> getValue() { ... }
>
> At the moment schemagen creates this XML:
>
> <xs:element name="containedProperties" minOccurs="0">
> <xs:complexType>
> <xs:choice maxOccurs="unbounded" minOccurs="0">
> <xs:element name="stringProperty" type="stringProperty"/>
> <xs:element name="integerProperty" type="integerProperty"/>
> <xs:element name="containerProperty" type="containerProperty"/>
> </xs:choice>
> </xs:complexType>
> </xs:element>
>
> What I want is that the <containedProperties/> tag is manditory and must contain at least
> one element. With the @XmlElement annotation I can just define the Bean property as
> required. How do I do this with @XmlElements and @XmlElementWrapper?

Upcoming JAXB 2.1 has @XmlElementWrapper.required() to do just that.


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com