Duration: 5 Minutes.
This tuorial illustrates how Smooks can be used to transform non-XML character based data streams.
Internally, Smooks deals with data as a W3C DOM, parsing it to a DOM using SAX. Smooks allows you to target a SAX XMLReader implementation at a specific message in the same way it allows you target any other transformation resource. Using this, we can write a SAX parser to parse any character stream, converting it to a stream of SAX events. That parser can then be targeted at all relevant messages using a profile (check out the other tutorials and Smooks docs for details on message targeting and profiling).
EDI as the Message Format
In this tutorial the message format will be an X12N EDI message as follows.
Implementing the X12N to SAX Event Parser
So we need to implement an X12N to SAX Event Parser that will convert the above X12N stream into a stream of SAX events. This class is the X12nToSaxEventParser.
X12nToSaxEventParser has 2 support class:
- X12nStreamReader: Wraps the stream and makes the X12N segments easier to access.
- X12nModel: Contains definitions to help the parser convert the X12N stream to a stream of SAX events.
Targeting the X12N to SAX Event Parser
x12n-config.cdrl illustrates how to configure the X12nToSaxEventParser to parse messages originating from X12N based message producers ("x12n-requester" - "x12n-producer" might have been a better profile name).
While the incoming message format may not be XML, as long as it's predictable, hierarchical etc, we can read an interpret the message as though it were an XML message. Once in XML, you can leverage the transformation and serialisation features of Smooks to generate whatever output is required.