GUIZOL Olivier / FTR&D / US wrote:
>
> Hi all,
> Some people have already sent about the same questions I am going to ask
> alas I found no reply.
> The problem I have is to unmarshal and marshal a sequence of objects that
> may be of different type( still they inherit from a same base class).
> Here is an example:
> 1) Node is the base class.
> 2) NodeA, NodeB and NodeC inherit from Node.
> 3) NodeA instance could have NodeB and NodeC instances as children
> an xml file would be:
> <nodeA>
> <nodeB/>
> <nodeB/>
> <nodeC/>
> <nodeB/>
> </nodeA>
> And the DTD would include <!ELEMENT nodeA (nodeB | nodeC)*>
>
> I would want the mapping file to set nodeA children from a single method (to
> get objects as well).
> So, I would want to know if it's possible using Castor!
Yes of course it's possible.
Simply use the following mapping:
...
<class...>
<field name="nodes" set-method="addChild" get-method="getChildren"
type="Node" collection="Vector">
<bind-xml auto-naming="deriveByClass"/>
</field>
</class>
...
The auto-naming="deriveByClass" will indicate that the XML name should
be derived by the Class mapping for each item in the collection.
Note that this feature was introduced in the CVS back on January 18th
and therefore is not available in Castor 0.9.3.9. So you'll need to use
a CVS version.
You can check out the CVS code normally (ie using the CVS head) or you
can use the following tags:
tag_0_9_3_16
tag_0_9_3_13
There are some other tags, but if you are going to check out from the
CVS you might as well use a more recent tag.
Thanks,
--Keith
> By now, I have found a work around to set objects as I want:
> The name attribute of a field element has to be unique but we can set a
> random name (I am using my own classes so I have no side-effects that could
> happen using the source generator tool) and set the "direct" attribute to
> "false". This way we can have the same set method for different object types
> as below:
>
> <class name="NodeA" extends="Node">
> <map-to xml="nodeA" />
> <field name="g_childrenB" -- in fact g_children --
> direct="false"
> set-method="addChild"
> type="NodeB"
> collection="vector"
> >
> <bind-xml name="nodeB" />
> </field>
> <field name="g_childrenC" -- in fact g_children --
> direct="false"
> set-method="addChild"
> type="NodeC"
> collection="vector"
> >
> <bind-xml name="nodeC" />
> </field>
> </class>
> ...
>
> But then I am stuck if I want to marshal again as I need a specific method
> for each NodeX (X = B or C) type to return a subset of g_children.
> I would have hope that there is an easier solution and if there is such
> thing please tell me!
>
> If you read this line thanks for reading it all.
> Thanks for your help.
>
> Olivier
>
> ------------------------------------------------------------------------
>
> Part 1.2 Type: application/ms-tnef
> Encoding: base64
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev