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
>
>
>