2. Sometime in our Mule flow we require to
transform a payload from one form to another.
For example in some cases, we need transform
an XML payload to JSON
3. Now there are several ways of transforming the XML
payload to JSON in Mule. You can use XML to Object
transformer and then Object to JSON transformer
in doing so.
But how about using a custom transformer to directly
transform XML to JSON ?
It will be a very easy way in achieving that without
using much transformer in our flow and can directly
transform end-to-end
4. But how can we use Custom transformer to
transform in Mule??
.
6. Let us consider we have a following Mule flow :-
Now you can see in the above flow the inbound endpoint will
pic a file that contains XML content from a location and put it
into another location and the outbound file will contain it’s
corresponding JSON content.
You can also see a custom transformer in the middle which
is responsible for this XML payload conversion to JSON
directly.
7. Now, let’s check the code for this flow :-
<flow name="CustomXMLToJSONTransformer"
doc:name="CustomXMLToJSONTransformer">
<file:inbound-endpoint path="E:backuptest"
responseTimeout="10000" doc:name="File">
<file:filename-regex-filter pattern="xmlFile.txt"
caseSensitive="false"/>
</file:inbound-endpoint>
<file:file-to-string-transformer doc:name="File to String"/>
<custom-transformer class="CustomXMLToJSONTransformer"
doc:name="XmlToJson"/>
<logger message="#[message.payload]" level="INFO"
doc:name="Logger" />
<file:outbound-endpoint path="E:backuptestnewfolder"
outputPattern="jsonFile.txt" responseTimeout="10000"
doc:name="File" />
</flow>
As you can see the file xmlFile.txt contains XML content that will be
converted directly into JSON by the custom-transformer into a file
jsonFile.txt
8. Now let’s check the XML content of file xmlFile.txt :-
So, the above is the XML content we need to convert using
our custom transformer into a corresponding JSON
9. So our custom transformer java class is the following :-
public class CustomXMLToJSONTransformer extends AbstractMessageTransformer implements
DiscoverableTransformer {
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
try {
String xml = (String) message.getPayload();
XmlMapper xmlMapper = new XmlMapper();
List entries = xmlMapper.readValue(xml, List.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(entries);
return json;
} catch (Exception e) {
System.out.println("Error: " + e);
e.printStackTrace();
}
return null;
}
@Override
public int getPriorityWeighting() {
return 0;
}
@Override
public void setPriorityWeighting(int weighting) {
}
}
10. Now let’s test our application . We will see the following in our Mule
console :-
You can see the payload is transformed into JSON and has been
dispatched to location E:backuptestnewfolder with file name
jsonFile.txt
11. Now if we open the file jsonFile.txt from location
E:backuptestnewfolder we will get our JSON content
as following
You can see you have generated the JSON for the XML directly
just using a simple custom transformer component