This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CSV to XML

 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • 0
  • 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
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • 0
  • 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.
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • 0
  • 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
Sheriff
Posts: 11962
5
  • 0
  • 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
 
Krishna Radha
Greenhorn
Posts: 25
  • 0
  • 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
Sheriff
Posts: 11962
5
  • 0
  • 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
  • 0
  • 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
Sheriff
Posts: 11962
5
  • 0
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic