[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Engineering » XML and Related Technologies
 
RSS feed
 
New topic
Author

Converting CSV to XML

Anton Melumad
Greenhorn

Joined: Jan 17, 2006
Messages: 2

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
Messages: 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
Messages: 2

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.
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Engineering » XML and Related Technologies
 
RSS feed
 
New topic
replay challenge