File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes CSV to XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "CSV to XML" Watch "CSV to XML" New topic
Author

CSV to XML

Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
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


Malhar Barai
SOA & Java Book
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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 of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
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-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Aug 30, 2001
Posts: 25
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Aug 30, 2001
Posts: 25
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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
 
subject: CSV to XML