Friday, August 30, 2013

CSV to XML transformation with WSO2 ESB Smooks Mediator

This post provides a sample CSV to XML transformation with WSO2 ESB. WSO2 ESB supports executing Smooks features through 'Smooks Mediator'. 

Latest ESB can be downloaded from here


We are going to transform the below CSV to an XML message.

Lakmali,Erandi,Female,20,SriLanka
Lakmali,Baminiwatta,Female,20,SriLanka

This is the format of the XML output message.
here

 
  Lakmali
  Erandi
  Female
  20
  SriLanka
 
 
  Lakmali
  Baminiwatta
  Female
  20
  SriLanka
 

First lets write the smooks configuration to transform above CSV to given XML message (smooks-csv.xml).


<smooks-resource-list
 xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
 xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.2.xsd">

   <resource-config selector="org.xml.sax.driver">
  <resource>org.milyn.csv.CSVReader</resource>
  <param name="fields">firstname,lastname,gender,age,country</param>
  <param name="rootElementName">people
  </param>
  <param name="recordElementName">person
   </param>
    </resource-config>
</smooks-resource-list>
Now let's write a simple proxy service to take the CSV file as the input message and process through the smooks mediator. For that first you need to enable VFS transport sender and reciever.

Below is the service synapse configuration. Make sure to change the following parameters according to your file system. You can find more information about the parameters from here.

  •   transport.vfs.FileURI
  •   transport.vfs.MoveAfterProcess
  •   transport.vfs.ActionAfterFailure



<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="CSVSmooks"
       transports="https,http,vfs"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <smooks config-key="smooks-csv">
            <input type="text"/>
            <output type="xml"/>
         </smooks>
         <log level="full"/>
      </inSequence>
   </target>
   <parameter name="transport.PollInterval">5</parameter>
   <parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
   <parameter name="transport.vfs.FileURI">file:///home/lakmali/dev/test/smooks/in</parameter>
   <parameter name="transport.vfs.MoveAfterProcess">file:///home/lakmali/dev/test/smooks/original</parameter>
   <parameter name="transport.vfs.MoveAfterFailure">file:///home/lakmali/dev/test/smooks/original</parameter>
   <parameter name="transport.vfs.FileNamePattern">.*.csv</parameter>
   <parameter name="transport.vfs.ContentType">text/plain</parameter>
   <parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
   <description/>
</proxy>

You have to make a ESB local entry with the key 'smooks-csv' and give path to smooks-csv.xml which we crated above. So in the smooks mediator above, we are loading the smooks config through the local entry key name (smooks-csv).

To perform the transformation what you need to do is drop the input message file to transport.vfs.FileURI location. In the log you can see the transformed message in XML!! Now you got the CSV message in XML in your synapse sequence. So you can perform any further mediation to this message such as send to some endpoint/database/file etc.