1
Orgnization{

private String name;
private String uniqueId;
private boolean selfRegEnabled;
private List<Address> addrList;

public void setAddress(Address a){..}
public void setName(String name){..}

}

Addess{
private String type;
private String line1;
private String line2;
private String line3;
private String city;
private String state;
private String zip;
private String country;
}

CSV Header Columns are as below

System.UniqueID,Name,EnableSelf-Registration,Addr1.Type,Addr1.Line1,Addr1.Line2,Addr1.Line3,Addr1.City,Addr1.State,Addr1.Zip,Addr1.Country,Addr2.Type,Addr2.Line1,Addr2.Line2,Addr2.Line3,Addr2.City,Addr2.State,Addr2.Zip,Addr2.Country,Addr3.Type,Addr3.Line1,Addr3.Line2,Addr3.Line3,Addr3.City,Addr3.State,Addr3.Zip,Addr3.Country

My question might be related to below link

OpenCSV CSV to JavaBean

I didn't see that thread has a proper answer (I am not sure if I miss any from that thread)

Can we achieve same thing with any of the existing csv libraries such as supercsv, opencsv?

If I am using supercsv - can I map System.UniqueID column of csv to systemUniqueID property of my bean

Community
  • 1
  • 1
Nagendra Busam
  • 235
  • 1
  • 5
  • 19

1 Answers1

1

You can certainly do this with Super CSV using CsvDozerBeanReader. See this example on the website.

It's also explained in a bit more detail on this SO answer.

You may also be interested in this recent question, as it demonstrates the different ways to achieve deep/indexed mapping with Super CSV (with and without using Dozer).

Following the CsvDozerBeanReader example on the website, to read the CSV from your question you would use a field mapping of:

final String[] fieldMapping = new String[]{
    "uniqueId",
    "name",
    "selfRegEnabled",
    "addrList[0].type",
    "addrList[0].line1",
    "addrList[0].line2",
    "addrList[0].line3",
    "addrList[0].city",
    "addrList[0].state",
    "addrList[0].zip",
    "addrList[0].country",
    "addrList[1].type",
    "addrList[1].line1",
    "addrList[1].line2",
    "addrList[1].line3",
    "addrList[1].city",
    "addrList[1].state",
    "addrList[1].zip",
    "addrList[1].country",
    "addrList[2].type",
    "addrList[2].line1",
    "addrList[2].line2",
    "addrList[2].line3",
    "addrList[2].city",
    "addrList[2].state",
    "addrList[2].zip",
    "addrList[2].country"
};

Also, because the selfRegEnabled field is a boolean, you'll need to use cell processors to transform the String value into a Boolean - to do this you'd use the ParseBool processor.

Community
  • 1
  • 1
James Bassett
  • 9,458
  • 4
  • 35
  • 68
  • I did figure out after browsing through your site. Thanks for the answer - it was really helpful. one quick question actually through csv record for selfRegEnabled column I am getting Yes/No - how can I map directly to boolean in my bean? – Nagendra Busam May 07 '13 at 20:44
  • You'll have to use the [ParseBool](http://supercsv.sourceforge.net/apidocs/org/supercsv/cellprocessor/ParseBool.html) cell processor, i.e. `new ParseBool("Yes","No)` for the selfRegEnabled column. If that's the only column that needs processing then you can just create the processor array (`CellProcessor[] processors = new CellProcessor[fieldMapping.length]`), then setup that one column (`processors[2] = new ParseBool("Yes","No")`). See the examples on the website for more info. – James Bassett May 07 '13 at 22:30
  • I will select as correct once I get required reputation :). One more question I want to validate whole csv record for logging purpose, as I have to write invalid records to a new csv file. Suppose I have csv record with 50 columns - i need to validate them & if some columns are invalid I need to put custom message (which should be aggreagate with respect to all invalid columns) and add it back to new csv file with actual data + new column for exceptions – Nagendra Busam May 08 '13 at 14:10
  • @JamesBassett I am facing the same problem but I have a question around this. How can I achieve this when my csv file contains large data set? Should I write all column name based on indexing? Is there any simplest way? – Vaibhav_Sharma Jul 10 '18 at 13:19