Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7247807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:03:19+00:00 2026-05-28T22:03:19+00:00

Using Smooks ( 1.4 ) to read CSVs and transform them into a Person

  • 0

Using Smooks (1.4) to read CSVs and transform them into a Person POJO.

The CSV consists of comma-separated records on each line, and each record has pipe-delimited fields:

Smith|John|45|male|Johnny|JSmith|JSmizzle,

Smith|Jane|43|female|Janey

etc. Thus each line represents a different person to create. First, a POJO:

public class Person
{
    private String lastName;
    private String firstName;
    private int age;
    private boolean isMale;
    private List<String> aliases;
}

My problem is with the List of aliases. Here are the vital parts from my XML configuration:

<reader class="org.milyn.csv.CSVReader">
    <param name="fields">lastName,fristName,age,gender,aliases</param>
    <param name="separator">&#124;</param>
    <param name="strict">false</param>
</reader>

<core:filterSettings type="SAX"/>

<jb:bean beanId="person" class="net.me.myproject.app.Person" createOnElement="csv-set/csv-record/">
    <jb:value property="lastName" data="csv-set/csv-record/lastName"/>
    <jb:value property="firstName" data="csv-set/csv-record/firstName"/>
    <jb:value property="isMale" data="csv-set/csv-record/gender"/>
    <jb:value property="age" data="csv-set/csv-record/age"/>
    <jb:wiring property="aliases" beanRefId="aliases"/>
</jb:bean>

<jb:bean beanId="aliases" class="java.util.ArrayList" createOnElement="???">
    <jb:wiring beanRefId="alias"/>
</jb:bean>

<jb:bean beanId="alias" class="java.util.String" createOnElement="???">
    ???
</jb:bean>

So where I’m choking is in getting the createOnElement configured correctly for the aliases ArrayList as well as each alias String. Thanks in advance to anybody who can nudge me in the right direction!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:03:19+00:00Added an answer on May 28, 2026 at 10:03 pm

    First of all, your CSVReader‘s “fields” attribute will be a hodge-podge of all fields contained inside the CSV file, irregardless of which POJO, list or type they map back to. Thus, some fields will be Person properties, and some fields will be aliases that actually belong to the separate aliases bean, which is of type java.util.ArrayList<String>.

    Your job is to tell Smooks how to map each field to the respective bean/list/component/type/etc, which means telling it what to do when it encounters each field.

    Smooks does not support this sort of “dynamic” field binding, where you can have 0+ CSV fields map back to an ArrayList, that itself will either be empty or populated. You must enumerate each field in the CSVReader, which means having an ArrayList of aliases that has a fixed size.

    Ergo, you must decide on a maximum number of aliases that can be associated with each Person, and account for them in the fields list:

    <reader class="org.milyn.csv.CSVReader">
        <param name="fields">lastName,fristName,age,gender,alias1,alias2,alias3</param>
        <param name="separator">&#124;</param>
        <param name="strict">false</param>
    </reader>
    

    This means that each CSV record must have a credible value for your 3 aliases. I would recommend having an “ignore” value, such as “%%%IGNORE%%%” so that your app logic can no to remove an list items that contain that value (after Smooks has finished performing the transformation that is).

    You might also want to check out Smooks’s built-in $ignore$ token, which might already do this or something like it.

    The last piece before we can tie everything together in a complete code example is to simply accept the sad fact that Smooks either does not (or does not publicly document) any ability to use List<String> in this kind of example. You must convert your POJO to use either to a List<StringBuffer> or a List<StringBuilder> for aliases, so that we may take advntage of a Smooks-JavaBean value element attribute called setterMethod.

    Altogether now:

    <jb:bean beanId="aliases" class="java.util.ArrayList" createOnElement="csv-set/csv-record">
        <jb:wiring beanRefId="alias1"/>
        <jb:wiring beanRefId="alias2"/>
        <jb:wiring beanRefId="alias3"/>
    </jb:bean>
    
    <jb:bean beanId="alias1" class="java.util.StringBuffer" createOnElement="csv-set/csv-record/alias1">
        <jb:value data="csv-set/csv-record/alias1" setterMethod="append" />
    </jb:bean>
    
    <jb:bean beanId="alias2" class="java.util.StringBuffer" createOnElement="csv-set/csv-record/alias2">
        <jb:value data="csv-set/csv-record/alias2" setterMethod="append" />
    </jb:bean>
    
    <jb:bean beanId="alias3" class="java.util.StringBuffer" createOnElement="csv-set/csv-record/alias3">
        <jb:value data="csv-set/csv-record/alias3" setterMethod="append" />
    </jb:bean>
    

    So every time we start parsing a new csv-record, we create both a person bean (as your initial code example shows perfectly) as well as an aliases bean. Then, over the course of parsing this record, we will find Person properties as well as alias1 through alias3. The aliasN fields get stored into the aliases bean, meanwhile the other Person field gets stored into the person bean. Finally, Smooks knows to “wire” the person and aliases beans together to create a Java Person object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using smooks library to parse csv files fast and easy, mapping them programmatically
I am writing a route using camel and smooks, with the producer as a
Using NLTK and WordNet , how do I convert simple tense verb into its
using MSQL 2005 I have a continuous set of flow measurements (averaged for each
I'd like to map the below ORDERS EDI message to xml using Smooks UNB+UNOA:3+9313938000631:ZZ+9343015000575:ZZ+110210:0926+1++++1'UNH+1+ORDERS:D:01B:UN:EAN010'BGM+220+R-95300561X+9'DTM+137:20110210:102'DTM+2:20110211:102'NAD+BY+9313938000631::9'NAD+ST+9216GR::92'NAD+SU+963725::92'LIN+1++19300601396099:SRV'QTY+21:4'PRI+1E:5.412'UNS+S'MOA+86:21.648'CNT+2:1'UNT+14+1'UNZ+1+1'
I have a Joo.java class and I want to serialize it into XML using
Using mercurial, I've run into an odd problem where a line from one committer
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.