6 Replies Latest reply on Jan 9, 2008 4:47 AM by tfennelly

    Smooks transformation of csv files

    plz

      Hi,

      I'm quite new to JBossESB and are currently trying to transform a csv file to a POJO and back again and i have a couple of questions:

      1. Is it possible to transform csv files without a defined number of fields - as in an item with an undefined number of subitems?
      2. Is it possible to transform directly from csv to POJO (without transforming to an intermediate XML structure)?
      3. How do i transform my POJO back to csv?

      Best Regards

        • 1. Re: Smooks transformation of csv files
          tfennelly

           

          "plz" wrote:
          1. Is it possible to transform csv files without a defined number of fields - as in an item with an undefined number of subitems?


          It only supports a fixed number of fields at the moment, but it would be easy to make it work both ways.

          "plz" wrote:
          2. Is it possible to transform directly from csv to POJO (without transforming to an intermediate XML structure)?


          Well it depends on what you mean by "XML Structure". v4.2.1 of the ESB uses Smooks v0.9, which only supports a DOM based processing model, which means it needs to build a DOM of the whole message (in memory) before it can process the message (e.g. inti POJOs). So it does need to create a DOM structure in memory, but doesn't need to create serialized XML structure and pass over it again (re-parse etc). I think one of the examples may do this, but that's just for illustration purposes.

          Smooks v1.0 also supports a SAX based processing model, which means it can bypass construction of a DOM and go straight from CSV/EDI/whatever ---> SAX Event Stream ---> POJO i.e. no intermediate structures of any kind.

          "plz" wrote:
          3. How do i transform my POJO back to csv?


          Easiest way is to use a template, which can generate whatever format you like. My preference is for a FreeMarker template (XSLT is a pain IMO). See the model-driven-basic tutorial.

          • 2. Re: Smooks transformation of csv files
            plz

            Thanks for the quick reply.

            I'm still a bit unsure about 2 though:
            My initial approach was to create 2 actions, one for the csv to xml conversion and one for the xml to pojo. As i read your answer it is unnecessary to do this in two steps. If you could point me to the example showing how this is done in one step (mentioned in your reply) it would be much appreciated!

            • 3. Re: Smooks transformation of csv files
              tfennelly

              I don't think we have an example of exactly that (i.e. CSV to Java), but there are a number of equivalent examples at http://milyn.codehaus.org/Tutorials:
              - "xml-to-java": Uses the default SAX Parser (XML).
              - "edi-to-java": Explicitly configures in the SAX Parser for EDI (see 1st resource config in smooks-config.xml).

              The exact same principal applies for processing a CSV message, accept for CSV you need to configure the CSV SAX Parser (CSVParser).

              See the Stream Parsers section of the Smooks User Guide.

              • 4. Re: Smooks transformation of csv files
                tfennelly

                I should add... easiest way of doing this is to first configure in the CSV Parser. Then just run the transform. It will produce a serialized form of the SAX event stream produced by the CSV parser. From this, you can can work out what bean configurations you need to add.

                • 5. Re: Smooks transformation of csv files
                  plz

                  Sounds promising... I think I need to do some tutorial reading though.

                  Thanks a lot!

                  • 6. Re: Smooks transformation of csv files
                    tfennelly

                     

                    "plz" wrote:
                    I think I need to do some tutorial reading though.


                    Download and run them...