|  |  |  | 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 map a collection of elements

Intended Audience
Prerequisites
Basic concept
Mapping file
XML output
References

## Intended Audience

Anyone who wants to map a collection of elements.

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

## Prerequisites

None.

## Basic concept

Assume you have two classes Order and OrderItem, where an order holds a list of order items.

    public class Order {

        private List orderItems;
        private String orderNumber;

        public List getOrderItems() {
            return orderItems;
        }
        public void setOrderItems(List orderItems) {
            this.orderItems = orderItems;
        }
        public String getOrderNumber() {
            return orderNumber;
        }
        public void setOrderNumber(String orderNumber) {
            this.orderNumber = orderNumber;
        }
    }

    public class OrderItem {

        private String id;
        private Integer quantity;

        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public Integer getQuantity() {
            return quantity;
        }
        public void setQuantity(Integer quantity) {
            this.quantity = quantity;
        }
    }

As shown above, the Order instance has a field 'OrderItems' to hold a list of OrderItem instances.

## Mapping file

Here's the mapping file to instruct Castor XML about the relation of those two classes, Order and OrderItem respectively:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <mapping>
       <class name="nl.vodafone.castorbinding.demo.Order">
          <map-to xml="Order" />
          <field name="OrderNumber" type="java.lang.String">
             <bind-xml name="orderNumber" node="attribute" />
          </field>
          <field name="OrderItems"
    	         type="nl.vodafone.castorbinding.demo.OrderItem"
    	         collection="arraylist">
             <bind-xml name="OrderItem" />
          </field>
       </class>
       <class name="nl.vodafone.castorbinding.demo.OrderItem">
          <field name="Id" type="java.lang.String">
             <bind-xml name="id" node="attribute" />
          </field>
          <field name="Quantity" type="java.lang.Integer">
             <bind-xml name="quantity" node="attribute" />
          </field>
       </class>
    </mapping>

## XML output

Using the Castor XML marshaller with the mapping file shown above, the XML generated by Castor XML might look as follows:

    <?xml version="1.0"?>
    <Order orderNumber="pelssers_20-2-2006_1">
    	<OrderItem id="1" quantity="15"/>
        <OrderItem id="3" quantity="20"/>
    </Order>

## References

- | XML mapping

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