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.
This is the format of the XML output message.
here
<proxy xmlns="http://ws.apache.org/ns/synapse"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
<people> <person number="1"> <firstname>Lakmali</firstname> <lastname>Erandi</lastname> <gender>Female</gender> <age>20</age> <country>SriLanka</country> </person> <person number="2"> <firstname>Lakmali</firstname> <lastname>Baminiwatta</lastname> <gender>Female</gender> <age>20</age> <country>SriLanka</country> </person> </people>
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
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>
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.
can you please make this tutorial more elaborative. step by step approach to achieve the transformation.
ReplyDeleteHi Rahul,
ReplyDeleteYou can get some insight from the below sample:
http://docs.wso2.org/display/ESB470/Sample+271:+File+Processing
I tried sample 271 with ESB 4.9.0
ReplyDeleteIt gives error on execution
INFO - LogMediator To: , WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:247f4674-cd22-461e-88c2-6f12693a7d67, Direction: request, MESSAGE = Executing default 'fault' sequence, ERROR_CODE = 0, ERROR_MESSAGE = XML error occurred while creating the Smooks configuration from the config key: smooks Caused by Failed to locate XSD resource '/META-INF/ns/synapse' on classpath. Namespace: 'http://ws.apache.org/ns/synapse'., Envelope:
Same here
DeleteThis comment has been removed by a blog administrator.
ReplyDelete