Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The problem</h2> <p>The default <strong>Object to XML</strong> transformer only supports the <strong>alias</strong> annotation for classes:</p> <pre><code>&lt;mulexml:object-to-xml-transformer doc:name="Object to XML"&gt; &lt;mulexml:alias name="myAlias" class="com.my.package.MyClass"/&gt; &lt;/mulexml:object-to-xml-transformer&gt; </code></pre> <p>and ignores every other annotation. There is a already an <a href="http://www.mulesoft.org/jira/browse/MULE-6364" rel="nofollow">issue</a> since July 2012 on the official bug tracker, but it's unresolved until now.</p> <h2>Solution: Custom XML to Object transformer</h2> <h3>1. Create the transformer</h3> <p>Create a custom transformer in Java which extends <code>AbstractMessageTransformer</code>:</p> <pre><code>public class ObjectToXml extends AbstractMessageTransformer { public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { XStream xstream = new XStream(); // The payload should be the object to transform into XML Object object = message.getPayload(); // Register all annotations in the XStream instance xstream.processAnnotations(object.getClass()); // Convert the object to XML and set it as payload message.setPayload(xstream.toXML(object)); return message; } } </code></pre> <h3>2. Use the custom transformer in your Mule flow</h3> <p>Add a <strong>Java Transfromer</strong> (not Java Component) to your flow:</p> <pre><code>&lt;custom-transformer class="com.my.package.ObjectToXml" doc:name="Object to XML"/&gt; </code></pre> <p>That's it! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload