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