<xml:object-to-xml-transformer name="ObjectToXml" acceptMuleMessage="true"/>
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:
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.
/*
* $Id$
*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
*
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.module.xml.functional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.module.client.MuleClient;
import org.mule.tck.junit4.FunctionalTestCase;
import org.custommonkey.xmlunit.XMLAssert;
import org.junit.Test;
public class SimpleXmlTransformerFunctionalTestCase extends FunctionalTestCase
{
public static final String SERIALIZED = "<org.mule.module.xml.functional.SimpleXmlTransformerFunctionalTestCase_-Parent>\n" +
" <child>\n" +
" <name>theChild</name>\n" +
" </child>\n" +
"</org.mule.module.xml.functional.SimpleXmlTransformerFunctionalTestCase_-Parent>";
@Override
protected String getConfigResources()
{
return "org/mule/module/xml/simple-xml-transformer-functional-test-flow.xml";
}
@Test
public void testXmlOut() throws Exception
{
MuleClient client = new MuleClient(muleContext);
client.dispatch("xml-in", SERIALIZED, null);
Parent parent = (Parent) request(client, "xml-object-out", Parent.class);
assertNotNull(parent);
assertNotNull(parent.getChild());
assertEquals("theChild", parent.getChild().getName());
}
@Test
public void testObjectXmlOut() throws Exception
{
MuleClient client = new MuleClient(muleContext);
client.dispatch("object-in", new Parent(new Child("theChild")), null);
String xml = (String) request(client, "object-xml-out", String.class);
System.out.println(xml);
XMLAssert.assertXMLEqual(SERIALIZED, xml);
}
protected Object request(MuleClient client, String endpoint, Class<?> clazz) throws MuleException
{
MuleMessage message = client.request(endpoint, 3000);
assertNotNull(message);
assertNotNull(message.getPayload());
assertTrue(message.getPayload().getClass().getName(), clazz.isAssignableFrom(message.getPayload().getClass()));
return message.getPayload();
}
public static class Parent
{
private Child child;
public Parent()
{
this(null);
}
public Parent(Child child)
{
setChild(child);
}
public Child getChild()
{
return child;
}
public void setChild(Child child)
{
this.child = child;
}
}
public static class Child
{
private String name;
public Child()
{
this(null);
}
public Child(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mxml="http://www.mulesoft.org/schema/mule/xml"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<mxml:object-to-xml-transformer name="objectToXml"/>
<mxml:xml-to-object-transformer name="xmlToObject"/>
<vm:endpoint name="xml-in" path="xml-in" exchange-pattern="one-way"/>
<vm:endpoint name="object-in" path="object-in" exchange-pattern="one-way"/>
<vm:endpoint name="xml-object-out" path="xml-object-out"
exchange-pattern="one-way"/>
<vm:endpoint name="object-xml-out" path="object-xml-out"
exchange-pattern="one-way"/>
<flow name="xml to object">
<inbound-endpoint ref="xml-in"/>
<!-- MULE-5038 -->
<vm:outbound-endpoint ref="xml-object-out">
<transformer ref="xmlToObject"/>
</vm:outbound-endpoint>
</flow>
<flow name="object to xml">
<inbound-endpoint ref="object-in"/>
<vm:outbound-endpoint ref="object-xml-out">
<transformer ref="objectToXml"/>
</vm:outbound-endpoint>
</flow>
</mule>