Georg,
how about writing up a new HOW-To document that explains the use of the
container attribute based upon your sample domain classes,and hosws what
would be (un)marshalled with and without use of that attribute ?
Werner
Werner Guttmann wrote:
> Georg,
>
> the following mapping does the trick (for me).
>
> <mapping>
> <class name="xml.georg2.Data">
> <map-to xml="data" />
> <field name="data" type="xml.georg2.A" collection="array">
> <bind-xml name="a" node="element" />
> </field>
> </class>
> <class name="xml.georg2.A">
> <field name="field" type="xml.georg2.B" container="true"
> collection="array">
> <bind-xml name="value" node="element" />
> </field>
> <field name="id" type="xml.georg2.C" container="true">
> <bind-xml name="c" />
> </field>
> </class>
> <class name="xml.georg2.B">
> <field name="value" type="integer" />
> </class>
> <class name="xml.georg2.C">
> <field name="id" type="java.lang.String">
> <bind-xml name="id" node="attribute"/>
> </field>
> <field name="name" type="java.lang.String">
> <bind-xml name="name" node="attribute"/>
> </field>
> </class>
> </mapping>
>
> Regards
> Werner
>
> Georg Federmann wrote:
>> Hello Werner,
>>
>> sorry, my mistake! The example data model was incorrect. In fact, the A type
>> has multiple references to data carrying objects of type B and one reference
>> to one object of type C, carrying various informations identifying the A
>> type object. So there is one C object for each A object, and the fields of
>> this C object shall be used for the attributes of the <a> element.
>>
>> Sorry for the false data model before. For more clearance I've added some
>> example data. I tried to keep the example data as concise as possible.
>>
>> thank you for your help,
>> best regards, Georg
>>
>> the mapping xml:
>> <mapping>
>> <class name="ab.Data">
>> <map-to xml="data"/>
>> <field name="data" type="ab.A" collection="array">
>> <bind-xml name="a" node="element"/>
>> </field>
>> </class>
>> <class name="ab.A">
>> <field name="field" type="ab.B" container="true" collection="array">
>> <bind-xml name="value" node="element"/>
>> </field>
>> <field name="id" type="ab.C" container="true">
>> <bind-xml name="id" node="attribute"/>
>> </field>
>> </class>
>> <class name="ab.B">
>> <field name="value" type="integer"/>
>> </class>
>> <class name="ab.C">
>> <field name="id" type="java.lang.String">
>> <bind-xml name="id"/>
>> </field>
>> </class>
>> </mapping>
>>
>> Castor's result:
>> <data>
>> <a id="[EMAIL PROTECTED]">
>> <value>15</value>
>> <value>8</value>
>> </a>
>> <a id="[EMAIL PROTECTED]">
>> <value>12</value>
>> <value>17</value>
>> <value>8</value>
>> <value>3</value>
>> </a>
>> </data>
>>
>> how I would need it:
>> <data>
>> <a id="C1" name="series1">
>> <value>15</value>
>> <value>8</value>
>> </a>
>> <a id="C2" name="series2">
>> <value>12</value>
>> <value>17</value>
>> <value>8</value>
>> <value>3</value>
>> </a>
>> </data>
>>
>> the data model:
>> public class A {
>> private B[] field;
>> private C id;
>>
>> public A () {};
>>
>> public A ( B[] bs, C id ){
>> setField ( bs );
>> setId ( id );
>> }
>>
>> public B[] getField () {
>> return field;
>> }
>>
>> public void setField ( B[] field ) {
>> this.field = field;
>> }
>>
>> public C getId () {
>> return id;
>> }
>>
>> public void setId ( C id ) {
>> this.id = id;
>> }
>> }
>>
>> public class C {
>> private String id;
>> private String name;
>>
>> public C (){}
>>
>> public C ( String id, String name ){
>> setId ( id );
>> setName ( name );
>> }
>>
>> public String getId () {
>> return id;
>> }
>> public void setId ( String id ) {
>> this.id = id;
>> }
>> public String getName () {
>> return name;
>> }
>> public void setName ( String name ) {
>> this.name = name;
>> }
>> }
>>
>> public class B {
>> private String id;
>> private int value;
>>
>> public B (){}
>>
>> public B ( int val, String id ){
>> setId ( id );
>> setValue ( val );
>> }
>>
>> public String getId () {
>> return id;
>> }
>>
>> public void setId ( String id ) {
>> this.id = id;
>> }
>>
>> public int getValue () {
>> return value;
>> }
>>
>> public void setValue ( int value ) {
>> this.value = value;
>> }
>> }
>>
>> public class MarshalTest {
>> public static void main ( String[] args ) throws IOException,
>> MappingException, MarshalException, ValidationException {
>> Mapping mapping = new Mapping ();
>> mapping.loadMapping ( "resources/abMapping.xml" );
>>
>> B[] b1 = new B[] { new B ( 15, "val1" ), new B ( 8, "val2" ) };
>> B[] b2 = new B[] { new B ( 12, "val3" ), new B ( 17, "val4" ), new B
>> ( 8, "val5" ), new B ( 3, "val6" ) };
>> C series1 = new C ( "C1", "series1" );
>> C series2 = new C ( "C2", "series2" );
>> A[] a1 = new A[] { new A ( b1, series1 ), new A ( b2, series2 ) };
>> Data data = new Data ( a1 );
>>
>> StringWriter writer = new StringWriter ();
>> Marshaller marshaller = new Marshaller ( writer );
>> marshaller.setMapping ( mapping );
>> marshaller.marshal ( data );
>>
>> System.out.println ( writer.toString () );
>> Writer output = new FileWriter ( new File ( "resources/abResult.xml"
>> ) );
>> output.write ( writer.toString () );
>> output.flush ();
>> output.close ();
>> }
>> }
>>
>> On Mon, Jun 9, 2008 at 12:54 PM, Werner Guttmann <[EMAIL PROTECTED]>
>> wrote:
>>
>>> Personally, I would move the 'id' attribute to the A class, as that's
>>> where it belongs to (semantically).
>>>
>>> Werner
>>>
>>> Werner Guttmann wrote:
>>>> Georg,
>>>>
>>>> I don't think it's possible to map a 'multi-valued' field such as the
>>>> 'id' field of B to a single occurence within the XML output.
>>>>
>>>> Werner
>>>>
>>>> Georg Federmann wrote:
>>>>> Hello Werner,
>>>>>
>>>>> thanks a lot for your reply!
>>>>> Here is the mapping as I am using it right now:
>>>>>
>>>>> <mapping>
>>>>> <class name="ab.Data">
>>>>> <map-to xml="data"/>
>>>>> <field name="data" type="ab.A" collection="array">
>>>>> <bind-xml name="a" node="element"/>
>>>>> </field>
>>>>> </class>
>>>>>
>>>>> <class name="ab.A">
>>>>> <field name="field" type="ab.B" container="true"
>>> collection="array">
>>>>> <bind-xml name="value" node="element"/>
>>>>> </field>
>>>>> </class>
>>>>>
>>>>> <class name="ab.B">
>>>>> <field name="value" type="integer"/>
>>>>> </class>
>>>>> </mapping>
>>>>>
>>>>> and this is the result generated by Castor:
>>>>>
>>>>> <data>
>>>>> <a>
>>>>> <value>15</value>
>>>>> <value>8</value>
>>>>> </a>
>>>>> <a>
>>>>> <value>12</value>
>>>>> <value>17</value>
>>>>> <value>8</value>
>>>>> <value>3</value>
>>>>> </a>
>>>>> </data>
>>>>>
>>>>> this is as far as I got, and it's already very beautiful. But the
>>> crucial
>>>>> step is to get the "id" field of the B type into the "id" attribute of
>>> the
>>>>> <a> element like in
>>>>>
>>>>> <data>
>>>>> <a id="series1">
>>>>> <value>15</value>
>>>>> <value>8</value>
>>>>> </a>
>>>>> <a id="series2">
>>>>> <value>12</value>
>>>>> <value>17</value>
>>>>> <value>8</value>
>>>>> <value>3</value>
>>>>> </a>
>>>>> </data>
>>>>>
>>>>> I'm very grateful for any help here!
>>>>>
>>>>> best regards, Georg
>>>>>
>>>>> I've added the sources of the java types, maybe it's of any help ...
>>>>>
>>>>> package ab;
>>>>> public class A {
>>>>> private B[] field;
>>>>>
>>>>> public A () {};
>>>>>
>>>>> public A ( B[] bs ){
>>>>> setField ( bs );
>>>>> }
>>>>>
>>>>> public B[] getField () {
>>>>> return field;
>>>>> }
>>>>>
>>>>> public void setField ( B[] field ) {
>>>>> this.field = field;
>>>>> }
>>>>> }
>>>>>
>>>>> package ab;
>>>>> public class B {
>>>>> private String id;
>>>>> private int value;
>>>>>
>>>>> public B (){}
>>>>>
>>>>> public B ( int val, String id ){
>>>>> setId ( id );
>>>>> setValue ( val );
>>>>> }
>>>>>
>>>>> public String getId () {
>>>>> return id;
>>>>> }
>>>>>
>>>>> public void setId ( String id ) {
>>>>> this.id = id;
>>>>> }
>>>>>
>>>>> public int getValue () {
>>>>> return value;
>>>>> }
>>>>>
>>>>> public void setValue ( int value ) {
>>>>> this.value = value;
>>>>> }
>>>>> }
>>>>>
>>>>> package ab;
>>>>> public class Data {
>>>>> private A[] data;
>>>>>
>>>>> public Data (){};
>>>>>
>>>>> public Data ( A[] data ){
>>>>> setData ( data );
>>>>> }
>>>>>
>>>>> public A[] getData () {
>>>>> return data;
>>>>> }
>>>>>
>>>>> public void setData ( A[] data ) {
>>>>> this.data = data;
>>>>> }
>>>>> }
>>>>>
>>>>> package ab;
>>>>> public class MarshalTest {
>>>>>
>>>>> public static void main ( String[] args ) throws IOException,
>>>>> MappingException, MarshalException, ValidationException {
>>>>> Mapping mapping = new Mapping ();
>>>>> mapping.loadMapping ( "resources/abMapping.xml" );
>>>>>
>>>>> B[] b1 = new B[] { new B ( 15, "series1" ), new B ( 8, "series1"
>>> )
>>>>> };
>>>>> B[] b2 = new B[] { new B ( 12, "series2" ), new B ( 17,
>>> "series2" ),
>>>>> new B ( 8, "series2" ), new B ( 3, "series2" ) };
>>>>> A[] a1 = new A[] { new A ( b1 ), new A ( b2 ) };
>>>>> Data data = new Data ( a1 );
>>>>>
>>>>> StringWriter writer = new StringWriter ();
>>>>> Marshaller marshaller = new Marshaller ( writer );
>>>>> marshaller.setMapping ( mapping );
>>>>> marshaller.marshal ( data );
>>>>>
>>>>> System.out.println ( writer.toString () );
>>>>> Writer output = new FileWriter ( new File (
>>> "resources/abResult.xml"
>>>>> ) );
>>>>> output.write ( writer.toString () );
>>>>> output.flush ();
>>>>> output.close ();
>>>>> }
>>>>> }
>>>>>
>>>>>
>>>>>
>>>>> On Mon, Jun 9, 2008 at 10:13 AM, Werner Guttmann <
>>> [EMAIL PROTECTED]>
>>>>> wrote:
>>>>>
>>>>>> Georg,
>>>>>>
>>>>>> what does your mapping file look like right now ? How have you tried to
>>>>>> map A and Bs ?
>>>>>>
>>>>>> Having said that, I guess this might be achievable, but I am not 100%
>>>>>> sure. Basically, you will have to use the container attribute (set to
>>>>>> false) on the class mapping for 'B', so that no <B> tags will be
>>>>>> rendered. Whether this will work with your additional requirement on
>>> the
>>>>>> aggregation by the value of the id attribute of B, not sure.
>>>>>>
>>>>>> Regards
>>>>>> Werner
>>>>>>
>>>>>> Georg Federmann wrote:
>>>>>>> Hi all,
>>>>>>>
>>>>>>> I need to marshal a given object model to a given xml, but I can't
>>>>>> persuade
>>>>>>> Castor to do as needed.
>>>>>>> This is the problem:
>>>>>>> I have two java types A and B where B has a field "id" and A holds
>>>>>>> references to B. Now I need the field "id" of B as an attribute of the
>>>>>> xml
>>>>>>> element <a>, that represents A.
>>>>>>>
>>>>>>> class A{
>>>>>>> B[] field;
>>>>>>> }
>>>>>>>
>>>>>>> class B{
>>>>>>> String id;
>>>>>>> int value;
>>>>>>> }
>>>>>>>
>>>>>>> a collection of objects of type A shall be marshalled to some xml like
>>>>>> this:
>>>>>>> <data>
>>>>>>> <a id="b1">
>>>>>>> <value>15</value>
>>>>>>> <value>8</value>
>>>>>>> </a>
>>>>>>> <a id="b2">
>>>>>>> <value>12</value>
>>>>>>> <value>17</value>
>>>>>>> <value>8</value>
>>>>>>> <value>3</value>
>>>>>>> </a>
>>>>>>> </data>
>>>>>>>
>>>>>>> Somehow I can't see, how I can split the fields of the B type into an
>>>>>>> attribute "id" of the <a> element and a child element <value> of the
>>> <a>
>>>>>>> element.
>>>>>>>
>>>>>>>
>>>>>>> I appreciate any help
>>>>>>>
>>>>>>> best regards, Georg
>>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> 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
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> 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
>
>
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email