License     Codehaus     OpenEJB     OpenJMS     OpenORB     Tyrex     

Old releases
  General
  Release 1.2
  Release 1.3rc1

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 list of elements at the root


Intended Audience
Prerequisites
Basic concept
Mapping file
Unmarshalling
Marshalling
References


Intended Audience

Anyone who wants to map a list of elements at the document root.

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

Prerequisites

None.

Basic concept

Assume you have a java.util.List (java.util.ArrayList) and an OrderItem classes, where the List instance simply holds a list of order items.

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

Mapping file

Here's the mapping file for Castor XML, showing the class mapping for the OrderItem class:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapping>
   <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>

Unmarshalling

Using the Castor XML Unmarshaller with the mapping file shown above, the code to unmarshal the XML shown below ...

<?xml version="1.0"?>
<order">
	<order-item id="1" quantity="15"/>
	<order-item id="3" quantity="20"/>
</order>

... into a variable of type java.util.ArrayList is shown subsequently:

InputSource mappingSource = ...;
Mapping mapping = new Mapping();
mapping.loadMapping(mappingSource);        

InputSource inputSource = ...;
Unmarshaller unmarshaller = new Unmarshaller(ArrayList.class);
unmarshaller.setMapping(mapping);
ArrayList orders = (ArrayList) unmarshaller.unmarshal(inputSource);        
		

Marshalling

Using the Castor XML Marshaller with the mapping file shown above, the code to marshal a List of OrderItem instances to XML is shown below.

PrintWriter writer = ...;
List orders = new ArrayList();

Mapping mapping = new Mapping();
mapping.loadMapping(mappingSource);        

Marshaller marshaller = new Marshaller(writer);
marshaller.setRootElement("orders");
marshaller.setMapping(mapping);
marshaller.marshal(orders);        
		

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.