users@jaxb.java.net

Re: JAXB annotation - depending on the content of an xml-attribute

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 14 May 2007 17:15:23 -0700

You can use XmlAdapter to first unmarshal <property> into some generic
class and then use its type value to instanciate the right Property subtype.

hari_ wrote:
> Hello!
>
> Can someone help me please with the following problem:
>
> My xml looks like the following example:
> ..
> <model>
> <property name=�boolProp�? type=”Boolean�? value=”true�?/>
> <property name=�intProp�? type=”Integer�? value=�?4�? minValue=�?1�?
> maxValue=?3�?/>
> </model>
> ..
>
> The model classes:
>
> abstract class Property {
> String name;
> String type;
> String value;
> //...
> }
>
> class BooleanProperty extends Property {
> // no additional members
> //...
> }
>
> class IntegerProperty extends Property {
> String minValue;
> String maxValue;
> //...
> }
>
> class Model {
> // @XmlElement or ?????
> List<Property> properties;
> //...
> }
>
> I don’t know how to annotate the List<Property> that JAXB creates the
> different model classes (Boolean or IntegerProperty) depending on the type
> attribute in the property-tag.
>
> Has anyone an idea how to do this?
>
> Thank you for your help!


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com