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 work with wrapper elements around collections


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


Intended Audience

How to work with wrapper elements around collections.

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

Prerequisites

None.

Basic concept

When you have a class that holds a collection of objects of the same type, and you want to wrap the XML produced for the collection members with an additional XML artefact, then read on.

Java entities

For example, assume you have the following Java class Foo:

package com.example;
import java.util.Collection;

public class Foo {
    private String name;
    private Collection children;

    public Foo(){}
    public Foo(String name){
        setName(name);
    }
    public Collection getChildren() {
        return children;
    }
    public void setChildren(Collection children) {
        this.children = children;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Required XML output and XML schema

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

<foo:foo xmlns:foo="http://example.com/foo" name="foo">
    <foo:children>
        <foo:foo name="foo1" />
        <foo:foo name="foo2" />
    </foo:children>
</foo:foo>

where the structure of the output is defined by the following XML schema:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://example.com/foo"
    xmlns:tns="http://example.com/foo" elementFormDefault="qualified">
    <complexType name="fooType">
        <sequence>
            <element name="children" minOccurs="0">
                <complexType>
                    <sequence>
                        <element name="foo" type="tns:fooType"
                            maxOccurs="unbounded">
                        </element>
                    </sequence>
                </complexType>
            </element>
        </sequence>
        <attribute name="name"></attribute>
    </complexType>
</schema>

Mapping file

then you would use a mapping like where the location attribute on the field mapping for children defines the wrapper XML artifact to be used:

<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
                         "http://castor.org/mapping.dtd">
<mapping>
    <class name="com.example.Foo" auto-complete="false">
        <map-to xml="foo" ns-uri="http://example.com/foo" ns-prefix="foo" />
        <field name="name" type="java.lang.String">
            <bind-xml name="name" node="attribute" />
        </field>
        <field name="children" collection="collection" type="com.example.Foo">
            <bind-xml name="foo" location="children" />
        </field>
    </class>
</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.