Read file into POJO

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Read file into POJO

Alfonso Via-Reque
Hello Commons Users,
I'm looking for a way to read files directly into POJOs.  The idea is that the file layout would be defined in an "properties"-type file (XML or something).  The layout could be delimited or fixed-width, with the layout file defining the columns and which POJO member variable they should be set to.
For example, a file might be tilde (~) delimited and follow this layout:ID~first name~last name~age~address~city~state~zip


I'd like to read it into a class:
public class Person {    private Integer id;
    private String firstName;
    private String lastName;    private Integer age;    private String address;    private String city;
    private String state;
    private String zip;

    // Getters and Setters ...
}

Is there a tool that do this?


ThanksAlfonso
Reply | Threaded
Open this post in threaded view
|

Re: Read file into POJO

Greg Thomas
It's not an Apache project, but Gestalt @
https://github.com/tonybaines/gestalt dos exactly that. You define a Java
interface, and Gestalt will create an object that implements interface
based on the contents of a .xml or .properties file.

Greg

On 19 January 2017 at 04:26, Alfonso Via-Reque <[hidden email]>
wrote:

> Hello Commons Users,
> I'm looking for a way to read files directly into POJOs.  The idea is that
> the file layout would be defined in an "properties"-type file (XML or
> something).  The layout could be delimited or fixed-width, with the layout
> file defining the columns and which POJO member variable they should be set
> to.
> For example, a file might be tilde (~) delimited and follow this
> layout:ID~first name~last name~age~address~city~state~zip
>
>
> I'd like to read it into a class:
> public class Person {    private Integer id;
>     private String firstName;
>     private String lastName;    private Integer age;    private String
> address;    private String city;
>     private String state;
>     private String zip;
>
>     // Getters and Setters ...
> }
>
> Is there a tool that do this?
>
>
> ThanksAlfonso
Reply | Threaded
Open this post in threaded view
|

Re: Read file into POJO

garydgregory
In reply to this post by Alfonso Via-Reque
JAXB is included in Java.

Gary

On Wed, Jan 18, 2017 at 8:26 PM, Alfonso Via-Reque <
[hidden email]> wrote:

> Hello Commons Users,
> I'm looking for a way to read files directly into POJOs.  The idea is that
> the file layout would be defined in an "properties"-type file (XML or
> something).  The layout could be delimited or fixed-width, with the layout
> file defining the columns and which POJO member variable they should be set
> to.
> For example, a file might be tilde (~) delimited and follow this
> layout:ID~first name~last name~age~address~city~state~zip
>
>
> I'd like to read it into a class:
> public class Person {    private Integer id;
>     private String firstName;
>     private String lastName;    private Integer age;    private String
> address;    private String city;
>     private String state;
>     private String zip;
>
>     // Getters and Setters ...
> }
>
> Is there a tool that do this?
>
>
> ThanksAlfonso




--
E-Mail: [hidden email] | [hidden email]
Java Persistence with Hibernate, Second Edition
<https://www.amazon.com/gp/product/1617290459/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617290459&linkCode=as2&tag=garygregory-20&linkId=cadb800f39946ec62ea2b1af9fe6a2b8>

<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1617290459>
JUnit in Action, Second Edition
<https://www.amazon.com/gp/product/1935182021/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182021&linkCode=as2&tag=garygregory-20&linkId=31ecd1f6b6d1eaf8886ac902a24de418%22>

<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182021>
Spring Batch in Action
<https://www.amazon.com/gp/product/1935182951/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182951&linkCode=%7B%7BlinkCode%7D%7D&tag=garygregory-20&linkId=%7B%7Blink_id%7D%7D%22%3ESpring+Batch+in+Action>
<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182951>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory
Reply | Threaded
Open this post in threaded view
|

Re: Read file into POJO

MASTRELLA STEFANO
In reply to this post by Alfonso Via-Reque
Hi Alfonso,

> -----Original Message-----
> From: Alfonso Via-Reque
> [mailto:[hidden email]]
> Sent: Thursday, January 19, 2017 5:27 AM
> To: [hidden email]
> Subject: Read file into POJO
>
> Hello Commons Users,
> I'm looking for a way to read files directly into POJOs.  The
> idea is that the file layout would be defined in an
> "properties"-type file (XML or something). The layout
> could be delimited or fixed-width, with the layout file
> defining the columns and which POJO member variable
> they should be set to.
> For example, a file might be tilde (~) delimited and follow
> this layout:ID~first name~last
> name~age~address~city~state~zip

On how to define the layout you can use:

- XML, you can rely on Commons Configuration
- ~ delimeted, rely on standard Java class FileInputReader
  and StringTokenizer

> I'd like to read it into a class:
> public class Person {    private Integer id;
>     private String firstName;
>     private String lastName;    private Integer age;    private
> String address;    private String city;
>     private String state;
>     private String zip;
>
>     // Getters and Setters ...
> }
>
> Is there a tool that do this?

take a look at Apache Commons BeanUtils

http://commons.apache.org/proper/commons-beanutils/

as stated in the user guide

http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.3/apidocs/org/apache/commons/beanutils/package-summary.html#standard

you can access your accessor method via

PropertyUtils.getSimpleProperty(anIstanceOfPerson,"firstName")

>
> ThanksAlfonso

Hope it helps.

S.
________________________________

Le informazioni contenute in questo messaggio di posta elettronica sono da considerarsi riservate e confidenziali. Il loro utilizzo è consentito esclusivamente al destinatario in indirizzo e ne è vietata la diffusione in qualunque modo eseguita, salvo che ne sia data espressa autorizzazione dal mittente. Nel caso in cui il messaggio Le fosse pervenuto per errore, La invitiamo gentilmente ad eliminarlo in modo permanente dopo averne dato tempestiva comunicazione al mittente e a non utilizzare in alcun caso il suo contenuto. Qualsivoglia utilizzo non autorizzato di questo messaggio e dei suoi eventuali allegati espone il responsabile alle relative conseguenze civili e penali.

This communication is confidential and the use of the contained information is allowed solely for the intended recipient. Its disclosure, distribution or copying is prohibited, unless expressly authorized by the sender. If you receive this communication by mistake please notify us and delete the message and its attachments. Anyone responsible for the unauthorized use of this message and its attachments is liable to face legal consequences.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Read file into POJO

Alfonso Via-Reque
In reply to this post by Greg Thomas
Thank you and Grazie to all that replied!

      From: Greg Thomas <[hidden email]>
 To: Commons Users List <[hidden email]>; Alfonso Via-Reque <[hidden email]>
 Sent: Thursday, January 19, 2017 2:31 AM
 Subject: Re: Read file into POJO
   
It's not an Apache project, but Gestalt @ https://github.com/tonybaines/gestalt dos exactly that. You define a Java interface, and Gestalt will create an object that implements interface based on the contents of a .xml or .properties file. 
Greg
On 19 January 2017 at 04:26, Alfonso Via-Reque <[hidden email]> wrote:

Hello Commons Users,
I'm looking for a way to read files directly into POJOs.  The idea is that the file layout would be defined in an "properties"-type file (XML or something).  The layout could be delimited or fixed-width, with the layout file defining the columns and which POJO member variable they should be set to.
For example, a file might be tilde (~) delimited and follow this layout:ID~first name~last name~age~address~city~state~ zip


I'd like to read it into a class:
public class Person {    private Integer id;
    private String firstName;
    private String lastName;    private Integer age;    private String address;    private String city;
    private String state;
    private String zip;

    // Getters and Setters ...
}

Is there a tool that do this?


ThanksAlfonso