Versions Compared


  • 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 an EDI 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

SVN - Download - Other Tutorials

Other Relevant Info:

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


Transforming from EDI to XML

This example illustrates how to hook the EdiSax ( EDI parser into a Smooks based transform (filter operation).  It's another example of how non-XML streams can be processed by Smooks (see the csv-to-xml example).

In this example, we simply configure in the EdiSax parser to process the EDI stream into XML.  We don't perform any other transforms on the underlying data. For an example of how other transform operations can be built on top of this edi-to-xml transform, see the edi-to-java example.

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

No Format
HDR*1*0*59.97*64.92*4.95*Wed Nov 15 13:45:28 EST 2006
ORD*1*1*364*The 40-Year-Old Virgin*29.98
ORD*2*1*299*Pulp Fiction*29.99

And this is the expected result of our transformation:

No Format
                <date>Wed Nov 15 13:45:28 EST 2006</date>
                <title>The 40-Year-Old Virgin</title>
                <title>Pulp Fiction</title>


The Smooks Configuration

We simply specify the SmooksEDIParser 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="">

	<!-- Configure the EDI Parser to parse the message stream into a stream of SAX events. -->
	<resource-config selector="org.xml.sax.driver">
		<param name="mapping-model">/example/edi-to-xml-order-mapping.xml</param>


Here's the edi mapping ("/src/main/java/example/edi-to-xml-order-mapping.xml"):

No Format
<?xml version="1.0" encoding="UTF-8"?>
<medi:edimap xmlns:medi="">

	<medi:description name="DVD Order" version="1.0" />

	<medi:delimiters segment="&#10;" field="*" component="^" sub-component="~" />

	<medi:segments xmltag="Order">

		<medi:segment segcode="HDR" xmltag="header">
			<medi:field xmltag="order-id" />
			<medi:field xmltag="status-code" />
			<medi:field xmltag="net-amount" />
			<medi:field xmltag="total-amount" />
			<medi:field xmltag="tax" />
			<medi:field xmltag="date" />

		<medi:segment segcode="CUS" xmltag="customer-details">
			<medi:field xmltag="username" />
			<medi:field xmltag="name">
				<medi:component xmltag="firstname" />
				<medi:component xmltag="lastname" />
			<medi:field xmltag="state" />

		<medi:segment segcode="ORD" xmltag="order-item" maxOccurs="-1">
			<medi:field xmltag="position" />
			<medi:field xmltag="quantity" />
			<medi:field xmltag="product-id" />
			<medi:field xmltag="title" />
			<medi:field xmltag="price" />



The following illustrations attempts to visually describe the mapping that takes place (see EDIParser):


Executing The Transformation

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

No Format
// Instantiate Smooks with the config...
Smooks smooks = new Smooks("smooks-config.xml");

// Filter the input message to the outputWriter...
smooks.filter(new StreamSource(messageIn), new StreamResult(messageOut));

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

See the example/ in the example source.