Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Next »

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

SVN - Download - Other Tutorials

Other Relevant Info:

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

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 javabean-populator 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 javabean-populator tutorial.

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

<?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>

    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".
    <resource-config selector="order">
        <param name="beanId">order</param>
        <param name="beanClass">example.model.Order</param>

    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.
    Set the new bean instance on the "order" bean.
    <resource-config selector="order">
        <param name="beanClass">example.model.Header</param>
        <param name="setOn">order</param>
        <param name="bindings">
            <!-- Header bindings... -->
            <binding property="orderId" selector="header order-id" />
            <binding property="orderStatus" type="Long" selector="header status-code" />
            <binding property="netAmount" type="BigDecimal" selector="header net-amount" />
            <binding property="totalAmount" type="BigDecimal" selector="header total-amount" />
            <binding property="tax" type="BigDecimal" selector="header tax" />
            <binding property="date" type="OrderDateLong" selector="header date" />

    Create an "example.beans.Customer" bean instance when we visit the start of the
    <customer-details> element.
    Set the new bean instance on the "header" bean.
    <resource-config selector="customer-details">
        <param name="beanClass">example.model.Customer</param>
        <param name="setOn">header</param>
        <param name="bindings">
            <!-- Customer bindings... -->
            <binding property="userName" selector="customer-details username" />
            <binding property="firstName" selector="customer-details name firstname" />
            <binding property="lastName" selector="customer-details name lastname" />
            <binding property="state" selector="customer-details state" />

    Create an "example.beans.OrderItem" bean instance when we visit the start of the <order-item> element.
    Add the OrderItem instances to a list (because there are potentially many of them on an order).
    Set the OrderItem list on the "order" bean.
    <resource-config selector="order-item">
        <param name="beanClass">example.model.OrderItem</param>
        <param name="addToList">true</param>
        <param name="setOn">order</param>
        <param name="bindings">
            <!-- OrderItem bindings... -->
            <binding property="quantity" type="Integer" selector="order-item quantity" />
            <binding property="productId" type="String" selector="order-item product-id" />
            <binding property="price" type="BigDecimal" selector="order-item price" />
            <binding property="title" selector="order-item title" />

    Configure a special decoder for the <date> field of the <header> - to set the date format.
    <resource-config selector="decoder:OrderDateLong">
        <param name="format">EEE MMM dd HH:mm:ss z yyyy</param>


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="">

	<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");
// Create an exec context - no profiles....
StandaloneExecutionContext executionContext = smooks.createExecutionContext();

// Filter the input message to the outputWriter, using the execution context...
DOMResult domResult = new DOMResult();
smooks.filter(new StreamSource(new ByteArrayInputStream(messageIn)), domResult, executionContext);

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

See the example/ in the example source.

  • No labels