|  |  |  | License Codehaus OpenEJB OpenJMS OpenORB Tyrex

Old releases
General
Release 1.3
Release 1.3rc1
Release 1.2

Main
Home
About
Features
Download
Dependencies
Reference guide
Publications
JavaDoc
Maven 2 support
Maven 2 archetypes
DTD & Schemas
Recent HTML changes
News Archive
RSS news feed
Project Wiki

Development/Support
Mailing Lists
SVN/JIRA
Contributing
Support
Continuous builds
Prof. services

Related projects
Spring ORM support
Spring XML factories
WS frameworks

XML
XML

XML Code Generator
XML Code Generator

JDO
Introduction
First steps
Using JDO
JDO Config
Types
JDO Mapping
JDO FAQ
JDO Examples
JDO HOW-TOs
Tips & Tricks
Other Features
JDO sample JAR

Tools
Schema generator

Advanced JDO
Caching
OQL
Trans. & Locks
Design
KeyGen
Long Trans.
Nested Attrs.
Pooling Examples
LOBs
Best practice

DDL Generator
Using DDL Generator
Properties
Ant task
Type Mapping

More
The Examples
3rd Party Tools
JDO Tests
XML Tests
Configuration

About
License
User stories
Contributors
Marketplace
Status, Todo
Changelog
Library
Contact
Project Name

## How to wrap a collection with a wrapper element

Intended Audience
Prerequisites
Basic concept
Java entities
Required XML output
Mapping file

## Intended Audience

Anyone who wants to wrap a collection with a wrapper element.

This document helps people to get familiar with the basic concepts and shows an example.

## Prerequisites

None.

## Basic concept

When you have a field which is a list collection, and you want for each element of the list collection to be encapsulated in a containing element, then set container="false" in the field element for the list collection. By setting this attribute Castor recognizes that you want an element wrapping the members of the list.

By default Castor treats collections as "containers". Containers to Castor are simple objects whose only purpose is to hold other objects. Containers are not considered "first-class" objects and therefore have no direct XML representation. By setting container="false", you are telling Castor that the collection is not be treated as a container, but rather a first class object which should have an XML-representation.

## Java entities

For example, if you have the following two classes:

    package org.castor.example;

    import java.util.ArrayList;
    import java.util.List;

    public class Cart {

        private List<Item> itemsList = new ArrayList<Item>();

        public List<Item> getItemsList() {
            return this.itemsList;
        }

        public void addItem(Item item) {
            this.itemsList.add(item);
        }
    }

    package org.castor.example;

    public class Item {

        private Integer count;
        private String name;

        public Integer getCount() {
            return this.count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

## Required XML output

and you want to have Castor generate XML which looks like:

    <cart>
       <items-list>
           <item>
               <name>broccoli</name>
               <count>1</count>
           </item>
           <item>
               <name>orange</name>
               <count>4</count>
           </item>
       </items-list>
    </cart>

## Mapping file

then you would use a mapping like:

    <class name="org.castor.example.Cart"
           auto-complete="true">
        <field name="itemsList"
               collection="arraylist"
               type="org.castor.example.Item"
               container="false">
            <bind-xml name="items-list"/>
        </field>
    </class>

<notice> Copyright © 1999-2005 ExoLab Group, Intalio Inc., and Contributors. All rights reserved. </notice>
<notice> Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. XML, XML Schema, XSLT and related standards are trademarks or registered trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web Consortium. All other product names mentioned herein are trademarks of their respective owners. </notice>