Contact Us 1-800-596-4880

XML-Object Transformers

This pair of transformers converts XML code to serialized objects and back again. For serialization of Java XML objects, see DomToXml Transformer.

Object to XML

The Object to XML transformer converts any object to XML using XStream. You configure this transformer using the <object-to-xml-transformer> element. It takes the standard transformer attributes plus one additional attribute, acceptMuleMessage, which specifies whether to serialize the whole message to XML and not just its payload. This is useful with transports such as TCP where message headers are not supported and would otherwise be lost.

For example:

<xml:object-to-xml-transformer name="ObjectToXml" acceptMuleMessage="true"/>

You can then reference this transformer from an endpoint:

<vm:inbound-endpoint path="another.queue" transformer-refs="ObjectToXml" />

XML to Object

The XML to Object transformer converts XML created by the Object to XML transformer in to a Java object graph using XStream. You configure this transformer using the <xml-to-object-transformer> element. It takes the standard transformer attributes.

For example:

<xm:xml-to-object-transformer name="XmlToObject" />

Testing the Transformers

The transformers can be tested using functional tests. For example, the following functional test uses FunctionalTestCase, which is part of Mule’s Test support, to test the Object to XML transformer.

public class MuleEndpointConfigurationTestCase extends FunctionalTestCase
{

    protected String getConfigResources()
    {
        return "org/mule/test/integration/test-endpoints-config.xml";
    }

...
    public void testComponent4Endpoints() throws Exception
    {
        // test inbound
        Service service = muleContext.getRegistry().lookupService("TestComponent4");
        assertNotNull(service);
        assertNotNull(service.getInboundRouter().getEndpoints());
        assertEquals(1, service.getInboundRouter().getEndpoints().size());
        ImmutableEndpoint endpoint = (ImmutableEndpoint)service.getInboundRouter().getEndpoints().get(0);
        assertNotNull(endpoint);
        assertEquals(VMConnector.VM, endpoint.getConnector().getProtocol().toLowerCase());
        assertEquals("queue4", endpoint.getEndpointURI().getAddress());
        assertFalse(endpoint.getTransformers().isEmpty());
        assertTrue(endpoint.getTransformers().get(0) instanceof ObjectToXml);
        assertTrue(endpoint instanceof InboundEndpoint);
    }
...
}