users@jaxb.java.net

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

From: Rowland Smith <rowland_at_river2sea.org>
Date: Tue, 31 Jul 2007 10:06:21 -0700 (PDT)

I think it'll be more obvious with some example XML.

...
<Bogus>
  <Action>
    <Install refreshInterval="..."/>
  </Action>
</Bogus>
...


Let's say my class Bogus contains an instance of a subclass of
AbstractAction called Install.

public class Install extends AbstractAction {
    ...
}

public class Bogus {

    private AbstractAction _action ;

    ...
}

If I serialize it as is, I end up with:

...
<Bogus>
    <Install refreshInterval="..."/>
</Bogus>
...

However, to satisfy the schema requirement, I need to insert an artificial
element called <Action>
as a child element of <Bogus>, and as the parent element of <Install>.


Thanks again,
Rowland


kohsuke wrote:
>
> 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
>
>
>

-- 
View this message in context: http://www.nabble.com/marshall-wrapper-element-that%27s-not-present-in-model-tf4179731.html#a11928401
Sent from the java.net - jaxb users mailing list archive at Nabble.com.