Milind Rao wrote:
> 
> I just started using Castor to convert Objects to XML and back and have a couple of 
> questions
> 
> I have the following Objects
> 
> public class Member
> {
>     private int iID
>     private Level iLevel;
> 
>     // Constructor is package-private
>     Member(theMID, int theLID) {iID = theID; iLevel = new Level(theLID);}
>     public int getID() {return iID;}
>     public Level getLevel() {return iLevel;}
> }
> 
> public class Level
> {
>     private int iID;
> 
>     // Constructor is package-private
>     Level(int theID) {iID = theID;}
>     public int getID() {return iID;}
> }
> 
> public class MemberFactory()
> {
>     public Member createMember(int theMID, int theLID)
>             {return new Member(theMID, theLID);}
> }
> 
> I created an xml mapping file
> <mapping>
>   <class name="test.Member" verify-constructable="false">
>     <map-to xml="MEMBER" />
>     <field name="ID" type="java.lang.Integer" >
>       <xml name="mid" node="attribute" />
>     </field>
>     <field name="Level" type="test.Level" >
>       <xml name="lid" node="attribute" />
>     </field>
>   </class>
> 
>   <class name="test.Level" verify-constructable="false">
>     <map-to xml="LEVEL" />
>     <field name="Id" type="java.lang.Integer" >
>       <xml name="lid" node="attribute" />
>     </field>
>   </class>
> </mapping>
> 
> 1. What I'd like to get on Marshalling a Member is an XML that looks like
>    <Member mid="2" lid="2">
>    ie. I want the level ID, not the Level object.  Do I have to write my own 
> FieldHandler for this
>    or is there some way to do this in the XML Mapping file?

You can do that in the XML Mapping file...

add reference="true" to the field mapping as such:

<field name="Level" type="test.Level">
   <bind-xml name="lid" node="attribute" reference="true"/>
</field>

and make sure you mark the identity field for the test.Level class
mapping:

<class name="test.Level" ... identity="Id"/>


Note: You'll still need at some point to marshal the actual Level
instances, and not just their IDs...otherwise during unmarshalling,
you'll run into problems because the Ids won't be resolvable.

> 
> 2. I want to use the MemberFactory for creating the Member.
>    Again, I suspect that I have to write my own FieldHandler.  Right?

If you want to use your factory, then you'll need to write a
FieldHandler.

> 
> 3. I'd really like to see constructors from elements.  I don't like to have set 
> methods in my
>     classes unless really required.  So using Castor is making me change my design 
> philosophy.
> 
> There are no sample files on how to write the FieldHandler.  I have gone through the 
> archives and got very
> confused.  Is there any sample code for FieldHandlers somewhere.

There should be samples in the archives...but I'll place one up on the
FTP server later when I get some time.

--Keith

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to