Or: http://www.castor.org/how-to-wrap-a-collection-with-a-wrapper-element.html
By setting container="false" you are overriding the default behaviour where Castor doesn't give collections their own element. So set container="false" on the field, then add a <bind-xml> element that names the wrapper element which will be placed around the items in the list. Craig. -- Craig Tataryn site: http://www.basementcoders.com/ podcast: http://www.basementcoders.com/?feed=podcast itunes: http://itunes.apple.com/podcast/the-basement-coders irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin twitter: craiger On 2010-07-15, at 7:59 AM, <[email protected]> wrote: > Have you tried using the location attribute? > > Link: > http://www.castor.org/reference/html-single/index.html#xml.mapping.mappingfile.locationattribute > > Regards, > > Martin > > Von: Fredrik Neisler [mailto:[email protected]] > Gesendet: Donnerstag, 15. Juli 2010 14:56 > An: [email protected] > Cc: [email protected] > Betreff: [castor-user] Marshalling: adding extra elements to the > XML-output-file > > I need help to add extra elements to the produced XML-output-file. > > The example below describes the problem: > > The Java-domain-classes > ================ > public class Order { > > private List<OrderItem> orderItems; > private Long orderId; > > public List getOrderItems() { > return orderItems; > } > > public void setOrderItems(ArrayList<OrderItem> orderItems) { > this.orderItems = orderItems; > } > > public String getOrderId() { > return orderId; > } > > public void setOrderId(Long orderId) { > this.orderId = orderId; > } > } > > public class OrderItem { > > private Long id; > private Integer quantity; > private String description; > > public Long getId() { > return id; > } > > public void setId(Long id) { > this.id = id; > } > > public Integer getOrderQuantity() { > return orderQuantity; > } > > public void setOrderQuantity(Integer orderQuantity) { > this.orderQuantity = orderQuantity; > } > > public String getDescription() { > return id; > } > > public void setDescription(String description) { > this.description = description; > } > } > > > The Castor-mapping-file > ======================= > <class name="mypackage.Order> > <map-to xml="Order"/> > > <field name="orderId" type="Long"> > <bind-xml name="OrderId" node="element"/> > </field> > > </field name="orderItems" class="mypackage.OrderItem" type="ArrayList"> > <bind-xml name="Item" node="element"/> > </field> > > </class> > > <class name="mypackage.OrderItem> > <map-to xml="Item"/> > > <field name="id" type="Long"> > <bind-xml name="Identity" node="element"/> > </field> > > </field name="quantity" type="integer"> > <bind-xml name="Quantity" node="element"/> > </field> > > </field name="description" type="String"> > <bind-xml name="Description" node="element"/> > </field> > </class> > > > Produced XML-output-file > ======================== > <?xml version="1.0" ?> > <OrderRoot> > <Order> > <OrderId>1</OrderId> > <Item> > <Identity>1</Identity> > <Quantity>2</Quantity> > <Description>Some description</Description> > </Item> > <Item> > <Identity>2</Identity> > <Quantity>4</Quantity> > <Description>Some description</Description> > </Item> > <Item> > <Identity>3</Identity> > <Quantity>9</Quantity> > <Description>Some description</Description> > </Item> > </Order> > <Order> > <OrderId>2</OrderId> > <Item> > <Identity>4</Identity> > <Quantity>50</Quantity> > <Description>Some description</Description> > </Item> > <Item> > <Identity>5</Identity> > <Quantity>3</Quantity> > <Description>Some description</Description> > </Item> > <Item> > <Identity>6</Identity> > <Quantity>7</Quantity> > <Description>Some description</Description> > </Item> > </Order> > </OrderRoot> > > > Wanted XML-output-file > ====================== > <?xml version="1.0" ?> > <OrderRoot> > <OrderList> <=WANTED ELEMENT > <Order> > <OrderId>1</OrderId> > <ItemList> <=WANTED ELEMENT > <Item> > <Identity>1</Identity> > <Quantity>2</Quantity> > <Description>Some description</Description> > </Item> > <Item> > <Identity>2</Identity> > <Quantity>4</Quantity> > <Description>Some description</Description> > </Item> > <Item> > <Identity>3</Identity> > <Quantity>9</Quantity> > <Description>Some description</Description> > </Item> > </ItemList> <=WANTED ELEMENT > </Order> > <Order> > <OrderId>2</OrderId> > <ItemList> <=WANTED ELEMENT > <Item> > <Identity>4</Identity> > <Quantity>50</Quantity> > <Description>Some description</Description> > </Item> > <Item> > <Identity>5</Identity> > <Quantity>3</Quantity> > <Description>Some description</Description> > </Item> > <Item> > <Identity>6</Identity> > <Quantity>7</Quantity> > <Description>Some description</Description> > </Item> > </ItemList> <=WANTED ELEMENT > </Order> > </OrderList> <=WANTED ELEMENT > </OrderRoot> > > > StaxEventItemWriter in Spring to produce the output. > I have tried just about everything in the castor-mapping-file to get these > extra elements to add an extra level to encapsulate the collections but > failed. > > Does anyone have a solution for this? > > Unfortunately I don't have the choice of manipulating the java-domain classes > since they are used to produce other kind of output too and I don't want to > make an extra XSL-transform just to add the missing elements. > > Please help me! > > Thanks in advance > Fred

