|  |  |  | 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 map a map/hashtable of elements Intended Audience Prerequisites Simple Maps Basic concept Mapping file XML output Nested Maps 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. ## Simple Maps ### 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 Map itemlist;

            public Map getItemlist() {
                return itemlist;
            }

            public void setItemlist(Map 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 Map 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 contained 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>
## Nested Maps It is equally possible to nest Collections/Arrays within Maps, incl. Maps within Maps. The following Java class has a member 'map' of type `java.util.Map`.
    public class MapOfArrays {

        private Map map;

        public Map getMap() {
            return this.map;
        }

        public void setMap(Map map) {
            this.map = map;
        }

    }
The following code shows how this `Map` instance is filled with `String` arrays before marshalling.
    this.map.put("key1", new String[] { "key1_value1", "key1_Value2" });
    this.map.put("key2", new String[] { "key2_value1", "key2_Value2" });
Based upon the following mapping file, ...
    <class name="MapOfArrays">
        <field name="map" collection="map">
            <bind-xml name="map">
                <class name="org.exolab.castor.mapping.MapItem">
                    <field name="key" type="java.lang.String">
                        <bind-xml name="string1" node="attribute" />
                    </field>
                    <field name="value" collection="array" type="java.lang.String">
                        <bind-xml name="nested" />
                    </field>
                </class>
            </bind-xml>
        </field>
    </class>
the following XML will be generated during marshalling.
    <?xml version="1.0" encoding="UTF-8"?>
    <map-of-arrays>
      <map string1="key1">
        <nested>key1_value1</nested>
        <nested>key1_Value2</nested>
      </map>
      <map string1="key2">
        <nested>key2_value1</nested>
        <nested>key2_Value2</nested>
      </map>
    </map-of-arrays>
## 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. |