aspose file tools*
The moose likes XML and Related Technologies and the fly likes Converting 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 "Converting CSV to XML" Watch "Converting CSV to XML" New topic
Author

Converting CSV to XML

Anton Melumad
Greenhorn

Joined: Jan 17, 2006
Posts: 4
I need to convert CSV flat file data to XML with nested elements. My current approach is to first convert the CSV to a "flat" XML and then apply XLST transformation to the latter in order to obtain the final XML.

I am looking for more elegant XSLT syntax, the one I am using now seems ugly and probably will not work in case of optional elements.

Thanks,

My CSV looks like that:

id, cust_name, cust_age, ...
1, "John", 45, ...
2, "Mary", 25, ...
...

My intermediate XML is like:
...
<record>
<id>1</id>
<cust_name>John</cust_name>
<cust_age>45</cust_age>
...
</record>
...

And my target XML should look like that:
...
<record>
<id>1</id>
<customer>
<name>John</name>
<age>45</age>
</customer>
...
</record>
...

My XSLT code for the above transformation is:
...
<xsl:template match="cust_name">
<customer>
<name>
<xsl:value-of select="."/>
</name>
<age>
<xsl:value-of select="../cust_age"/>
</age>
</customer>

</xsl:template>

<xsl:template match="cust_age"/>
...
bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204
Here are some references, the 'servingxml' tool looks pretty good, please let us know if any of these work!

http://servingxml.sourceforge.net/

http://www.oio.de/public/konverter/csv2xml.htm

http://www.xmlsoftware.com/convert.html

http://www.sun.com/software/xml/developers/instancegenerator/
Anton Melumad
Greenhorn

Joined: Jan 17, 2006
Posts: 4
I have tried http://servingxml.sourceforge.net/ and it seemed to meet my needs completely. It covers large range of conversions, requires only short learning curve (at least for basic transformations) and the author, Daniel Parker, provides excellent support. While testing this library I noticed a bug which Daniel has fixed the very next weekend.
Praveen Chadichal
Greenhorn

Joined: Apr 07, 2009
Posts: 21
Hi,
Anton Melumad can you please help me in undersanding the setting in getting the xml file out of the CSV file. I tried to look for the getting started link of this but could not understand on it. It would be of great help if you can give me some insite on this. If you can give me a demo project also would help in great amount.


Thanks in advance,
Praveen
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Anton hans't posted here for more than 5 years; I doubt that he's following this topic.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting CSV to XML