From Smooks

Jump to: navigation, search

This tutorial is an extension of the edi-to-xml  and xml-to-java  tutorials. It illustrates how to go from (transform) an EDI message to a populated Java Object Model.


Executing the example is quite simple. From the example root folder, execute:

  1. "mvn clean install"
  2. "mvn exec:java"

Example source can be viewed and/or checked out from SVN here.

Get Smooks Training Today


(Back to Examples Index Page...)

Transforming from an EDI to Javabean Object Graph

Something to note when looking at this example is that Smooks doesn't perform this EDI-to-Java transform in a 2 step "pipeline" like process. It doesn't generate and intermediate XML, which it then has to parse and process etc. Smooks takes the EDI message and, using the EdiSax parser, goes straight to a DOM representation of the EDI message. This is then used by Smooks, through the Javabean Cartridge, to populate the Java Object Graph.

If you haven't already looked at the xml-to-java  tutorial, you may be interested in looking at that before looking at this example.

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

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 (i.e. a simple System.out of the populated Object Model  - see the toString method in the Order class ):

==============EDI as Java Object Graph=============
Order Header:
Customer: Fletcher, Harry
Date: Wed Nov 15 19:45:28 CET 2006
Details: ID=1, Status=0, Total=64.92
Order Items:
(0): ProductID=364, Quantity=1, Title='The 40-Year-Old Virgin', Price=29.98
(1): ProductID=299, Quantity=1, Title='Pulp Fiction', Price=29.99

This transformation could be taken a step further, whereby the EDI populated Object Model is used as part of a Model Driven Transformation. See the model-driven-basic  tutorial.

The Smooks Configuration

The Smooks configuration for this transformation contains:

  1. A configuration for generating an XML model (SAX event stream) from the input EDI, as with the edi-to-xml  tutorial.
  2. A set of Javabean population configuration for populating the Object Model , as with the xml-to-java  tutorial.

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

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
Configure the EDI Reader to parse the message stream into a stream of SAX events.
<edi:reader mappingModel="/example/edi-to-xml-order-mapping.xml" />
Create an "example.beans.Order" bean instance when we visit the start of the <order> element.
Assign the new bean instance to the beanId of "order".
The "header" and "orderItemList" beans are wired in.
<jb:bean beanId="order" class="example.model.Order" createOnElement="order">
<jb:wiring property="header" beanIdRef="header" />
<jb:wiring property="orderItems" beanIdRef="orderItemList" />
Create an "example.beans.Header" bean instance when we visit the start of the <order> element.
Note, we're creating this bean instance before we encounter the actual <header> element.
This bean is wired into the "order" bean.
<jb:bean beanId="header" class="example.model.Header" createOnElement="order">
<jb:wiring property="customer" beanIdRef="customer" />
<jb:value property="orderId" data="header/order-id" />
<jb:value property="orderStatus" data="header/status-code" />
<jb:value property="netAmount" data="header/net-amount" />
<jb:value property="totalAmount" data="header/total-amount" />
<jb:value property="tax" data="header/tax" />
<jb:value property="date" decoder="Date" data="header/date">
<jb:decodeParam name="format">EEE MMM dd HH:mm:ss z yyyy</jb:decodeParam>
Create an "example.beans.Customer" bean instance when we visit the start of the
<customer-details> element.
This bean is wired into the "header" bean.
<jb:bean beanId="customer" class="example.model.Customer" createOnElement="customer-details">
<!-- Customer bindings... -->
<jb:value property="userName" data="customer-details/username" />
<jb:value property="firstName" data="customer-details/name/firstname" />
<jb:value property="lastName" data="customer-details/name/lastname" />
<jb:value property="state" data="customer-details/state" />
Create an ArrayList when we visit the start of the <order> element.
The "orderItem" beans are wired into this list and this list is wired into the "order" bean.
<jb:bean beanId="orderItemList" class="java.util.ArrayList" createOnElement="order">
<jb:wiring beanIdRef="orderItem" />
Create an "example.beans.OrderItem" bean instance when we visit the start of the <order-item> element.
This bean is wired into the "orderItemList" ArrayList.
<jb:bean beanId="orderItem" class="example.model.OrderItem" createOnElement="order-item">
<!-- OrderItem bindings... -->
<jb:value property="quantity" data="order-item/quantity" />
<jb:value property="productId" data="order-item/product-id" />
<jb:value property="price" data="order-item/price" />
<jb:value property="title" data="order-item/title" />

Here's the edi mapping ("/src/main/java/example/edi-to-xml-order-mapping.xml"). See the edi-to-xml  tutorial for more details on this mapping:

<?xml version="1.0" encoding="UTF-8"?>
<medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.0.xsd">
<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" />

Executing The Transformation

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

// Instantiate Smooks with the config...
Smooks smooks = new Smooks("smooks-config.xml");
try {
JavaResult result = new JavaResult();
// Filter the input message to the JavaResult...
smooks.filterSource(new StreamSource(messageIn), result);
return (Order) result.getBean("order");
} finally {

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

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