commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Bourg <ebo...@apache.org>
Subject Re: [csv] Headers
Date Thu, 15 Mar 2012 00:58:53 GMT
There is another alternative, we might replace the records returned as a 
String[] by a CSVRecord class able to access the fields by id or by 
name. This would be similar to a JDBC resultset (except for the looping 
logic)

This avoids the duplication of the parser, which might still be 
generified later to support custom beans.

The example becomes:

   CSVFormat format = CSVFormat.DEFAULT.withHeader();

   for (CSVRecord record : format.parse(in)) {
       Person person = new Person();
       person.setName(record.get("name"));
       person.setEmail(record.get("email"));
       person.setPhone(record.get("phone"));
       persons.add(person);
   }

The record is not a Map to keep it simple, it only exposes 3 methods: 
get(int), get(String) and size()

Emmanuel Bourg



Mime
View raw message