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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * $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;
        }
    }
}

         
      
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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>