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 use the Spring FactoryBeans for Castor XML

Documentation Author(s):
Werner Guttmann
Steven Dolg




Prerequisites

The following sections assume that you have a valid Castor XML mapping for a Java entity named Product as follows:

<mapping>

  <!--  Mapping for Product  -->
  <class name="org.springframework.xml.entity.Product" identity="id">
    <map-to xml="product" />
	<field name="id" type="integer">
		  <bind-xml name="id" node="element"/>
		</field>
    <field name="name" type="string">
      <bind-xml name="name" node="element" />
    </field>
  </class>

</mapping>			
			

The sources for this Product entity are as follows:

public class Product {

    private int id;
    private String name;
    
    public int getId() {
        return this.id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
			

How to use the CastorMarshallerFactoryBean

Before you'll be able to obtain an instance of Marshaller through Spring, you have to add a Spring bean definition to your Spring configuration as follows:

<bean id="resolver" class="org.springframework.xml.castor.CastorResolverFactoryBean"/>

<bean id="marshaller"
      class="org.springframework.xml.castor.CastorMarshallerFactoryBean">
   <property name="resolver"><ref local="resolver"/></property>
</bean>
            

Based upon this configuration, you will be able to obtain a Marshaller instance as follows:

ApplicationContext context = ....;
Marshaller marshaller = (Marshaller) this.context.getBean("marshaller");            
            

The Marshaller instance obtained in this way does not have any mapping information associated, and will thus use the introspection mechanism to establish a mapping between the Java object and the XML representations.

With the above Marshaller instance, you can set e.g. a java.io.Writer and simply start the marshalling process as shown below:

        Writer out = new StringWriter();
        marshaller.setWriter(out);
        marshaller.marshal(product);
            

How to use the CastorUnmarshallerFactoryBean

Before you'll be able to obtain an instance of Unmarshaller through Spring, you have to add a Spring bean definition to your Spring configuration as follows:

<bean id="resolver" class="org.springframework.xml.castor.CastorResolverFactoryBean"/>
            
<bean id="unmarshaller"
      class="org.springframework.xml.castor.CastorUnmarshallerFactoryBean">
   <property name="resolver"><ref local="resolver"/></property>
</bean>
    	    

Based upon this configuration, you will be able to obtain a Unmarshaller instance as follows:

ApplicationContext context = ....;
Unmarshaller unmarshaller = (Unmarshaller) this.context.getBean("unmarshaller");            
            

The Unmarshaller instance obtained in this way does not have any mapping information associated, and will thus use the default introspection mechanism to establish a mapping between the Java object and the XML representations.

With the above Unmarshaller instance, you can unmarshal the following XML document instance as shown subsequently:

<?xml version="1.0" encoding="UTF-8"?>
<product>
	<name>blah</name>
	<id>1</id>
</product>
            

Product product = (Product) unmarshaller.unmarshal(new InputSource(resource));
assertNotNull(product);
assertEquals(1, product.getId());
assertEquals("name", product.getName());
             

How to specify a mapping file

To specify that the Castor (Un)Marshaller instances should use a custom mapping file (in addition to the default introspection mechanism), please amend above bean definition for the 'resolver' bean as follows:

<bean id="resolver" class="org.springframework.xml.castor.CastorResolverFactoryBean">
  <property name="castorProperties">
    <props>
      <prop key="mappingLocation">src/test/resources/mapping.xml</prop>
    </props>
  </property>
</bean>	   	
	   	

 
   
  
   
 


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.