Thanks. It works!
When wrappers elements will be supported? Damiano Pezzotti ........................................ Software Architect | Alambitco tel +39 333 2116258 fax +39 02 700432209 email [EMAIL PROTECTED] web www.alambitco.com pgp public key -----Messaggio originale----- Da: Keith Visco [mailto:[EMAIL PROTECTED]] Inviato: gioved� 11 aprile 2002 21.56 A: [EMAIL PROTECTED] Oggetto: Re: [castor-dev] XML mapping problem > Pezzotti Damiano wrote: > > Hi, > I have a problem. > > I have this XML code : > > <PAGE> > <DAY> > <NUMBER>05</NUMBER> > <DATE>2002-03-17</DATE> > <DAY> > ...... > > > > I have the object Page that has 2 fields , number and date, but I > haven't the ogject Day. > > How can I write the mapping file? > > <mapping> > <class name="Page"> > <map-to xml="PAGE"/> > > <field name="date" type="java.lang.String"> > <bind-xml name="??????" /> > </field> > > <field name="number" type="java.lang.String"> > <bind-xml name="???" /> > </field> > Currently, "wrapper elements" are not supported. So you'll need to do some extra work to get it working. 1. You can use XSLT to remove (unwrap) the <DAY> element. 2. -Or- you can create a Day object for your object model. 3. -Or- you can use a special FieldHandler so that the Day object is not needed for your object model, only for unmarshalling and marshalling. I think number 3 is the best approach because it doesn't require altering your XML or your object model. To use number 3... 1. create a Day object (only used for marshalling/unmarshalling, not part of the object model) 2. create a FieldHandler as such: --- import org.exolab.castor.mapping.FieldHandler; import org.exolab.castor.mapping.ValidityException; /** * The FieldHandler for the "day wrapper" field of the Page class. **/ public class DayHandler implements org.exolab.castor.mapping.FieldHandler { /** * Creates a new DayHandler for the Page class **/ public DayHandler() { super(); } /** * Returns the value of the field from the object. * * @param object The object * @return The value of the field * @throws IllegalStateException The Java object has changed and * is no longer supported by this handler, or the handler is not * compatiable with the Java object */ public Object getValue( Object object ) throws IllegalStateException { Page page = (Page)object; Day day = new Day(); day.setDate(page.getDate()); day.setNumber(page.getNumber()); return day; } /** * Sets the value of the field on the object. * * @param object The object * @param value The new value * @throws IllegalStateException The Java object has changed and * is no longer supported by this handler, or the handler is not * compatiable with the Java object * @thorws IllegalArgumentException The value passed is not of * a supported type */ public void setValue( Object object, Object value ) throws IllegalStateException, IllegalArgumentException { Page page = (Page)object; Day day = (Day)value; page.setDate(day.getDate()); page.setNumber(day.getNumber()); } /** * Sets the value of the field to a default value. * <p> * Reference fields are set to null, primitive fields are set to * their default value, collection fields are emptied of all * elements. * * @param object The object * @throws IllegalStateException The Java object has changed and * is no longer supported by this handler, or the handler is not * compatiable with the Java object */ public void resetValue( Object object ) throws IllegalStateException, IllegalArgumentException { //-- add reset code here } /** * @deprecated No longer supported */ public void checkValidity( Object object ) throws ValidityException, IllegalStateException { //-- add validation check if desired } /** * Creates a new instance of the object described by this field. * * @param parent The object for which the field is created * @return A new instance of the field's value * @throws IllegalStateException This field is a simple type and * cannot be instantiated */ public Object newInstance( Object parent ) throws IllegalStateException { return new Day(); } } --- 3. modify your mapping as such: <mapping> <class name="Page"> <map-to xml="PAGE"/> <field name="day" type="Day" handler="DayHandler"> <bind-xml name="DAY"/> </field> </class> </mapping> Hope that helps, --Keith ----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to [EMAIL PROTECTED] with a subject of: unsubscribe castor-dev ----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to [EMAIL PROTECTED] with a subject of: unsubscribe castor-dev
