Hi Werner, yes a parent could hold multiple children.  And in that case each 
Child is listed under the <children> container.  So you'll have say 5 children 
from parent 1234, then you'll have say 4 children from parent 6789 listed.

<?xml version="1.0" encoding="UTF-8" ?>
<children>
        <child>
                <id>4321</id>
                <name>Child of Parent 1234</name>
                <parentId>1234</parentId>
                <parentName>Parent 1234</parentName>
        </child>
        <child>
                <id>1212</id>
                <name>Another Child of Parent 1234</name>
                <parentId>1234</parentId>
                <parentName>Parent 1234</parentName>
        </child>

<!-- here's where the parent would have changed, so we are now serializing 
children of a new parent -->

        <child>
                <id>2121</id>
                <name>A Child of Parent 3211</name>
                <parentId>3211</parentId>
                <parentName>Parent 3211</parentName>
        </child>

        <child> 
        .
         .
         .
        </child>

        .
          .
          .
</children>

Castor would be fed a Collection: Collection<Parent> and asked to serialize it 
as above.

If Castor can't do it like that, I think what I'll do is create an intermediary 
"flattened" object and write a simple mapping for that.  So literally an object 
called "ParentChild" that has attributes from each, and I'll compile all the 
Children from each parent into this ParentChild collection and feed that to 
Castor. (i.e. Collection<ParentChild>)

Craig.


On 2010-12-17, at 2:25 AM, Werner Guttmann wrote:

> Hi Craig,
> 
> does a Parent instance really hold more than one instance of Child objects ? 
> Or does it just happen that there's a List<Child>, but there will never be 
> more than one instance is this list ?
> 
> In case there can be more than one Child instance in this List<Child>, how 
> would the resulting XML actually look like ?
> 
> Cheers
> Werner
> 
> On 16.12.2010 21:42, Craig Tataryn wrote:
>> I was wondering if I could get some guidance on how to do the following:
>> 
>> I have a Class like so:
>> 
>> public class Parent {
>> 
>>      private int id;
>>      private String name;
>>      private Collection children = new ArrayList();
>>      
>>      //setters and getters
>> 
>> }
>> 
>> public class Child {
>> 
>>      private int id;
>>      private String name;
>> 
>>      //setters and getters
>> }
>> 
>> I need Castor to marshal to an XML file like so:
>> 
>> <?xml version="1.0" encoding="UTF-8" ?>
>> <children>
>>      <child>
>>              <id>4321</id>
>>              <name>Child 4321</name>
>>              <parentId>1234</parentId>
>>              <parentName>Parent 1234</parentName>
>>      </child>
>> 
>>      <child> 
>>      .
>>       .
>>       .
>>      </child>
>> 
>>      .
>>        .
>>        .
>> </children>
>> 
>> So essentially Castor will be given a list of "Parent"s which each contain a 
>> list of "Child"ren. Instead of having Castor spit out the normal hierarchy, 
>> where a parent contains a list of child object, I need castor to just 
>> iterate through all children of each parent and spit out the child 
>> attributes with their associated parent attributes.
>> 
>> Hopefully I'm making sense (even if the reason why I'd want to do this 
>> remains a mystery).
>> 
>> Thanks,
>> 
>> Craig.
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>> 
>>     http://xircles.codehaus.org/manage_email
>> 
>> 
>> 


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to