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

 
   
  
   
 


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.