Unknown macro: {toptoc}
Castor Transformers
Castor transformers allows Mule message payloads to be serialized to XML and back again using the Castor XML binding framework.
CastorXmlToObject Transformer
This transformer uses Castor to serialize a XML to a Object. The CastorXmlToObject transformer provides a set of properties which can be set:
| Property |
Description |
Required |
| rootClass |
The root class defines the associated object class of the root object in the XML file. |
true |
| mappingFile |
The mapping file can be used to do advanced mapping strategies, provided by Castor. Castor XML mapping is a way to simplify the binding of java classes to XML document. It allows to transform the data contained in a java object model into/from an XML document. For more information see Castor XML Mapping. |
false |
In order to use the transformer, just simple register the transformer in you mule configuration file. The returnClass defines the result class, which is serialized by the transformer:
<transformer name="XmlToObject"
className="org.mule.transformers.xml.castor.CastorXmlToObject"
returnClass="org.mule.transformers.xml.castor.Entity">
<properties>
<property name="rootClass" value="org.mule.transformers.xml.castor.Entity"/>
</properties>
</transformer>
Here is an example XML file, representing the Entity class:
<?xml version="1.0" encoding="UTF-8"?>
<entity age="37">
<name>werner</name>
</entity>
Here is the associated Java Object, which should be mapped:
public class Entity implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
When you want to use your own mapping strategies in the transformer, just provide your personal mapping file and associate this file with the transformer:
<?xml version="1.0" ?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<class name="org.mule.transformers.xml.castor.Entity">
<map-to xml="entity" />
<field name="age" type="integer" >
<bind-xml name="age" node="attribute"/>
</field>
<field name="name" type="string">
<bind-xml name="name" />
</field>
</class>
</mapping>
Associate the mapping file with the transformer:
<transformer name="MappedXmlToObject"
className="org.mule.transformers.xml.castor.CastorXmlToObject"
returnClass="org.mule.transformers.xml.castor.Entity">
<properties>
<property name="rootClass" value="org.mule.transformers.xml.castor.Entity"/>
<property name="mappingFile" value="mapping.xml"/>
</properties>
</transformer>
ObjectToCastorXml Transformer
To serialize a object to XML is simple as defining the CastorObjectToXml transformer. As previous, a optional mapping file can be used by the transformer, by setting the mappingFile property. This mapping file can be used to modifiy the result XML.
<transformer name="ObjectToXml" className="org.mule.transformers.xml.castor.CastorObjectToXml"/>