License     Codehaus     ExoLab     OpenEJB     OpenJMS     OpenORB     Castor     Tyrex     
 

Main
  Home
  About
  Features
  Download
  API
  Schema
  Mailing Lists
  CVS/JIRA
  Contributing
  Support
  RSS news feed
  Recent changes

XML
  Using XML
  Source Generator
  Schema Support
  XML Mapping
  XML FAQ
  XML HOW-TOs
  Custom Handlers

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

Advanced JDO
  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 map/hashtable of elements


Intended Audience
Prerequisites
Basic concept
Mapping file
XML output
References


Intended Audience

Anyone who wants to map a Map/Hashtable 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 Items and Item, where an Items instance holds a Map/Hashtable of Item instances.

public class Items {

    private Hashtable itemlist;
    
    public Hashtable getItemlist() {
        return itemlist;
    }
    
    public void setItemlist(Hashtable itemlist) {
        this.itemlist = itemlist;
    }
}

public class Item {

    private String id;
    private String description;
    private String supplier;
    private String contact;
    
    public String getContact() {
        return contact;
    }
    public void setContact(String contact) {
        this.contact = contact;
    }
    public String getSupplier() {
        return supplier;
    }
    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

As shown above, the Items instance has a field 'itemList' to hold a Hashtable of Item instances.

Mapping file

Here's the mapping file to instruct Castor XML about the relation of those two classes, Items and Item respectively:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapping>
  <class name="nl.vodafone.castorbinding.demo.Items">
	  <map-to xml="Items"/>
	  <field name="Itemlist" collection="hashtable">
		  <bind-xml name="MyItem">
			  <class name="org.exolab.castor.mapping.MapItem">
				  <field name="key" type="java.lang.String">
					  <bind-xml name="id" node="attribute"/>
				  </field>
				  <field name="value" type="nl.vodafone.castorbinding.demo.Item">
					  <bind-xml name="Item"/>
				  </field>	  
			  </class>
	      </bind-xml>		  
	  </field>  	  	  
  </class>	
  <class name="nl.vodafone.castorbinding.demo.Item">
	  <map-to xml="Item"/>
	  <field name="Id" type="java.lang.String">
		  <bind-xml name="id" node="element"/>
	  </field>
	  <field name="Description" type="java.lang.String">
		  <bind-xml name="description" node="element"/>
	  </field>
	  <field name="Supplier" type="java.lang.String">
		  <bind-xml name="supplier" node="element"/>
	  </field>
	  <field name="Contact" type="java.lang.String">
		  <bind-xml name="contact" node="element"/>
	  </field>	  	  	  
  </class>	
</mapping>

Please note the use of the org.exolab.castor.mapping.MapItem definition within the <bind-xml> element in above mapping to map the elements containeed in the Map/Hashtable.

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" encoding="UTF-8" standalone="yes"?>
<Items>
   <MyItem id="1">
      <Item>
        <id>1</id>		
        <description>desc1</description>
        <supplier>supp1</supplier>
        <contact>cont1</contact>			
      </Item>
   </MyItem>
   <MyItem id="2">
      <Item>
         <id>2</id>		
         <description>desc2</description>
         <supplier>supp2</supplier>
         <contact>cont2</contact>			
      </Item>
   </MyItem>
   <MyItem id="3">
      <Item>
         <id>3</id>		
         <description>desc3</description>
         <supplier>supp3</supplier>
         <contact>cont3</contact>			
      </Item>
   </MyItem>
</Items>

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.