The CSV format is a most popular one used as data input of java program. How to parser CSV format file is a common task for many java developers.
I found a short road to a short road to transform CSV to POJO using SAXON, XSLT and Castor. This will be helpful for some tasks such as data generation of java project using CSV file as input.
First, transform CSV to XML using a without hard coding schema in xsl soluation. Please refer to Resource [1].
Then, using Castor to do XML binding. Please refer to Resource [2] and [3].
It’s very easy to do Java-to-XML binding using Castor.
It’s very easy to do Java-to-XML binding using Castor. For example,
// Marshal and save to XML file
FileWriter file = new FileWriter("person.xml");
Marshaller m = new Marshaller(file);
m.marshal(person);
file.close();
// Read from XML and unmarshal
FileReader uFile = new FileReader("person.xml");
Unmarshaller u = new Unmarshaller();
Person uPerson = (Person)u.unmarshal(Person.class, uFile);
Resources
[1] CSV to XML converter: A CSV to XML converter in XSLT 2.0.
[2] Data binding with Castor: Install and setup Castor
[3] XML to POJO using Castor: Java to XML and Back Again with Castor XML
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment