25

How can I convert XML file to EDI file using SMOOKS?

I am able to convert EDI to XML, in fact this is part of examples provided by SMOOKS.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Shirish Bathe
  • 627
  • 9
  • 22
  • The Smooks framework is no longer under active development - but if have restrictions of using old code - the source github has always had the example to convert EDI to XML-> https://github.com/smooks/smooks/tree/master/smooks-examples/edi-to-xml – somshivam Apr 06 '19 at 13:24

1 Answers1

1

Based on your question i tried to do some research on it . Please check if it helps you .

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
CUS*user1*Harry^Fletcher*SD
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:

<Order>
    <header>
            <order-id>1</order-id>
            <status-code>0</status-code>
            <net-amount>59.97</net-amount>
            <total-amount>64.92</total-amount>
            <tax>4.95</tax>
            <date>Wed Nov 15 13:45:28 EST 2006</date>
    </header>
    <customer-details>
            <username>user1</username>
            <name>
                    <firstname>Harry</firstname>
                    <lastname>Fletcher</lastname>
            </name>
            <state>SD</state>
    </customer-details>
    <order-item>
            <position>1</position>
            <quantity>1</quantity>
            <product-id>364</product-id>
            <title>The 40-Year-Old Virgin</title>
            <price>29.98</price>
    </order-item>
    <order-item>
            <position>2</position>
            <quantity>1</quantity>
            <product-id>299</product-id>
            <title>Pulp Fiction</title>
            <price>29.99</price>
    </order-item>
</Order>

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"):

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

<!--
Configure the EDI Reader to process the message stream into a stream of SAX events.
-->
<edi:reader mappingModel="/example/edi-to-xml-order-mapping.xml" />

</smooks-resource-list>

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

<?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>
    <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>
        <medi:field xmltag="state" />
    </medi:segment>
    <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" />
    </medi:segment>
</medi:segments>
</medi:edimap>

Executing The Transformation:

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

try {
// Filter the input message to the outputWriter...
smooks.filterSource(new StreamSource(messageIn), new
StreamResult(messageOut));
} finally {
smooks.close();
}
Siddhartha
  • 492
  • 1
  • 5
  • 15