Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

This tutorial illustrates is how to generate a stream of SAX events from a CSV stream and how that stream of SAX events feeds into Smooks to generate an XML model that can be transformed further (in this case it is not).

Table of Contents
indent20px
styledisc

 
SVN - Download - Other Tutorials

Other Relevant Info:

 
To Build: "mvn clean install"
To Run: "mvn exec:java"

Anchor
transformer
transformer

Transforming from CSV to XML

This is a very simple example that illustrates how Smooks to configure in a non-XML stream parser (CSV) into Smooks.

So here's the source csv file that is to be transformed:

No Format
Tom,Fennelly,Male,4,Ireland
Mike,Fennelly,Male,2,Ireland

And this is the expected result of our transformation (as generated by the CSVParser):

No Format
<csv-set>
	<csv-record>
		<firstname>Tom</firstname>
		<lastname>Fennelly</lastname>
		<gender>Male</gender>
		<age>4</age>
		<country>Ireland</country>
	</csv-record>
	<csv-record>
		<firstname>Mike</firstname>
		<lastname>Fennelly</lastname>
		<gender>Male</gender>
		<age>2</age>
		<country>Ireland</country>
	</csv-record>
</csv-set>

Anchor
configuration
configuration

The Smooks Configuration

We simply specify the CSVParser as the stream parser. More transformation configurations could be added to transform this message further.

Here's the configuration ("smooks-config.xml"):

No Format
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">

	<!--Configure the CSVParser to parse the message into a stream of SAX events. -->
	<resource-config selector="org.xml.sax.driver">
		<resource>org.milyn.csv.CSVParser</resource>
		<param name="fields" type="string-list">firstname,lastname,gender,age,country</param>
	</resource-config>

</smooks-resource-list>

Anchor
execution
execution

Executing The Transformation

Again, it's exactly the same as with the java-basic tutorial:

No Format
Smooks smooks = new Smooks("smooks-config.xml");
StringResult result = new StringResult();

smooks.filter(new StreamSource(messageIn), result);

Of course, you'd typically cache the Smooks instance.

See the example/Main.java in the example source.