Access Keys:
Skip to content (Access Key - 0)
Unknown macro: {toptoc}

Castor configuration for Mule 2

Castor transformers allows Mule message payloads to be serialized to XML and back again using the Castor XML binding framework. Castor transformers make use of the new provided schema based configuration. Therefore it becomes easier for developers to configure their transformers. The source code for Mule 2 Castor module can be found here.

<mule xmlns="http://www.mulesource.org/schema/mule/core/2.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:castor="http://www.mulesource.org/schema/mule/castor/2.0"
       xsi:schemaLocation="
               http://www.mulesource.org/schema/mule/core/2.0 http://www.mulesource.org/schema/mule/core/2.0/mule.xsd
               http://www.mulesource.org/schema/mule/castor/2.0 http://www.mulesource.org/schema/mule/castor/2.0/mule-castor">

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:

<castor:xml-to-object name="XmlToObject" rootClass="org.mule.transformers.xml.castor.Entity"/>

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:

<castor:object-to-xml name="MappedObjectToXml" mappingFile="mapping.xml"
                               rootClass="org.mule.transformers.xml.castor.Entity"/>

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.

<castor:object-to-xml name="ObjectToXml"/>
Adaptavist Theme Builder (3.3.3-conf210) Powered by Atlassian Confluence 2.10, the Enterprise Wiki.
Free theme builder license