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