Markus,
> 2) When a child is created, the parent element is on the sax-stack. Castor
> could use that information and call the setParent() method with the
> reference from the stack. There could be a definition like
This would have to be implemented in Castor.
However, it would mean adding more 'magic' to Castor, which already
has quite a good portion of it.
> But how can I tell castor to use the addChild() method? Setting the
> set-method attribute of the children-field element does not work,
> because castor seems to ignore the set-method if the field is a
> collection. Castor seems to call the get-method and add any new
> child directly to the collection.
This works perfectly in our application. Here's an example excerpt from our mapping
file:
<field name="NodeList"
type="com.skynamics.prisma.core.process.Node"
collection="collection"
get-method="getNodeList"
set-method="addNode">
<bind-xml name="node"/>
</field>
Castor will use the addNode method to add new elements.
Note that the method name must start with 'add' in order to be regonized as
single element add method and not as list setter.
> category.setParent(category) ;
Hmmm, should this mean
> category.setParent(this) ;
HTH,
Heiko Erhardt
skynamics AG
www.skynamics.com
> -----Original Message-----
> From: Barchfeld, Markus [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, June 06, 2002 4:00 PM
> To: [EMAIL PROTECTED]
> Subject: [castor-dev] Back-reference from child to parent in xml?
>
>
> Hello everybody.
>
> I want to use castor for mapping a simple xml file to an object tree
> and vice versa. The mapping of an object tree is quite easy to create,
> when every node has references to its children, but a child does *not*
> have a back-reference to its parent.
> If I want to have a back-reference, I could use references. But because I
> want to keep the XML file as simple as possible ( people should be able
> to modify it without using a tool) I want to avoid the references.
> And that should be possible, because the XML structure contains
> the information already. But I did not find a way so far, althout I could
> imagine two ways. Let explain my example first.
>
>
> Java class :
>
> public class Category {
> private String name ;
> private Category parent ;
> private Vector children ;
>
> // getters and setters ...
>
> }
>
>
> The corresponding xml is :
>
> <Category name="all">
> <Category name="child1">
> </Category>
> <Category name="child2">
> </Category>
> </Category>
>
>
> The mapping file:
>
> <mapping>
> <class name="Category">
> <field name="name" type="string">
> <bind-xml name="name" node="attribute"/>
> </field>
> <field name="children" type="Category"
> collection="collection">
> <bind-xml name="Category"/>
> </field>
> </class>
> </mapping>
>
> Possible solutions:
> 1) In order to establish a back-reference, I could tie together the adding
> of the children with the creation of the back-reference. E.g.
>
> public void addChild(Category category) {
> this.getCategories().addCategory(category) ;
> category.setParent(category) ;
> }
>
> But how can I tell castor to use the addChild() method? Setting the
> set-method attribute of the children-field element does not work,
> because castor seems to ignore the set-method if the field is a
> collection. Castor seems to call the get-method and add any new
> child directly to the collection.
>
> 2) When a child is created, the parent element is on the sax-stack. Castor
> could use that information and call the setParent() method with the
> reference from the stack. There could be a definition like
>
> <mapping>
> <class name="Category">
> <field name="parent" type="Category">
> <bind-xml reference="parent-element"/>
> </field>
> </class>
> </mapping>
>
>
> Is there a solution to my problem or do I have to use references in my
> example?
>
> thanks
>
> Markus
>
> --
> Markus Barchfeld / EDS Deutschland / E Solutions
> Eisenstr. 58
> 65428 R�sselsheim
> ++49-6142-80-1436
>
> -----------------------------------------------------------
> 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