Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

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:


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

You can then reference this transformer from an endpoint:


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


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


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
    }
...
}