• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

CSV to XML

 
Author
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a req. to convert CSV data to XML & also to validate it with the schema.
how do I go around this...???
Hope I am clear.
TIA
MB
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, Google for a CSV parsing library and write the piece of code that reads a .csv file into an object hierarchy of your choice.
Once you've got this working, move on to writing the XML document. I'd suggest doing it "by hand", meaning that you just get a PrintWriter etc and do writer.println("<?xml version='1.0'?>") and so forth.
Finally, you can proceed to validating the resulting XML against a schema.
Small steps. That's all it takes.
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
First, Google for a CSV parsing library and write the piece of code that reads a .csv file into an object hierarchy of your choice.


I kind of like mine, located just a bit down the page under Code and Utilities.
-Jeff-
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeff Langr:
I kind of like mine, located just a bit down the page under Code and Utilities.

Heh. I've got one also. I guess every other Java developer does
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Once you've got this working, move on to writing the XML document. I'd suggest doing it "by hand", meaning that you just get a PrintWriter etc and do writer.println("<?xml version='1.0'?>") and so forth.



Is there any better solution that doing it "by hand".. ? Maybe once we read the CSV file using the CSV parsing utility, if we have an existing xsd, just create objects of this xsd and fill that up and serialize to get the xml.

If anyone has any better solutions on how to convert CSV to XML, please suggest.

Thanks
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also just create a DOM tree and serialize that. It should be a lot easier than generating Java code from an XML Schema document, etc.
 
Krishna Radha
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of explicitly populating the XML ourselves, are there any packages available to do that?

Can we use XSLT to specify template for conversion of CSV to XML? I see XSLT being used from XML to CSV.. Can we use the other way as well?

If so, how can this be done?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Krishna Radha:
Can we use XSLT to specify template for conversion of CSV to XML? I see XSLT being used from XML to CSV.. Can we use the other way as well?

Unfortunately XSL is only applicable for processing well-formed XML documents.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Instead of explicitly populating the XML ourselves, are there any packages available to do that?


I suppose you could create a class following JavaBean conventions to hold the CSV data items and then use java.beans.XMLEncoder to write XML. That would probably only work for simple data.
Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!