License     Codehaus     OpenEJB     OpenJMS     OpenORB     Tyrex     
 

Main
  Home
  About
  Features
  Download
  Maven 2 support
  API
  DTD & Schemas
  Recent changes
  RSS news feed

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

Related projects
  Spring ORM support
  Spring XML factories

XML
  Using XML
  XML Mapping
  XML FAQ
  XML HOW-TOs
  Custom Handlers
  Best practice

XML Code Generator
  Code Generator
  Properties
  Custom bindings
  Ant task
  Schema Support
  Example

JDO
  Introduction
  Using JDO
  JDO Config
  Types
  JDO Mapping
  JDO FAQ
  JDO Examples
  JDO HOW-TOs
  Other Features
  JDO sample JAR

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

More
  Presentations
  The Examples
  3rd Party Tools
  JDO Tests
  XML Tests
  Configuration
  Tips & Tricks
  Full JavaDoc
  CastorWiki
 
 

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


Copyright © 1999-2005 ExoLab Group, Intalio Inc., and Contributors. All rights reserved.
 
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.