Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting CSV to XML

 
Anton Melumad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anton hans't posted here for more than 5 years; I doubt that he's following this topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic