users@jaxb.java.net

Re: marshall wrapper element that's not present in model

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Tue, 31 Jul 2007 09:13:50 -0700

Rowland Smith wrote:
> I'm using JAXB 2.1 to go from my domain model to XML (and vice-versa).
>
> Let's say my model is the following, where Install and Remove are subclasses
> of AbstractAction:
>
> AbstractAction
> ^
> |
> |--Install
> |--Remove
>
> In the schema that I have to support, the concrete AbstractAction subclasses
> are 'wrapped' in an Action element:
>
> <Action>
> <Install refreshInterval="..."/>
> </Action>
>
> The domain model has no need for aggregation of an AbstractAction within
> some 'Action' class - and I don't want to pollute it with unnecessary
> classes just to make it serialize correctly.
>
> What's the correct approach to insert this 'wrapper' Action element which
> will contain a real AbstractAction element for Install or Remove when
> marshalling to XML?

I'm not quite sure what your question is, but take a look at
@XmlElementRef javadoc, as maybe what you are asking is relevant.

>
> Unmarshalling?
>
> Is this a job for an XmlAdapter?
>
> Schema example - Note that I can't change the schema.
>
> <xs:element name="Action">
> <xs:complexType>
> <xs:sequence>
> <xs:choice>
> <xs:element ref="Install"/>
> <xs:element ref="Remove"/>
> </xs:choice>
> <xs:choice>
> <xs:element name="InstallTime" >
> <xs:complexType>
> <xs:attribute name="expire" use="optional" type="xs:time"/>
> </xs:complexType>
> </xs:element>
> </xs:choice>
> </xs:sequence>
> <xs:attribute name="name" use="required" type="xs:string"/>
> </xs:complexType>
> </xs:element>
>
> TIA,
> Rowland
>
>


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com