Re: CSV to XML



On Oct 25, 2008, at 1:41 PM, Bee Tard wrote:

Great forum, been a reader for a long time. My first post comes as a
request for some help.

I am trying to process a CSV of undetermined length (want to know
how to
incorporate 'wc -l').

The CSV has 4 columns that I want to map to XML nodes.

I am having trouble writing each CSV line to an individual XML file
(100
lines = 100 XML files). The first CSV element in each line would be
the
xml file name.

Seems pretty straight forward but I am pulling my hair out trying to
make it work. Any help would be greatly appreciated. I have googled
everywhere for it. I appreciate any help in advance!

How about something like this:

$ cat for_xml.csv
filename
one
two
three
$ cat csv2xml.rb
#!/usr/bin/env ruby -wKU

require "rubygems"
require "faster_csv"

FCSV.foreach(ARGV.shift, :headers => true) do |row|
File.open(row["filename"] + ".xml", "w") do |f|
# create XML data here...
end
end
$ ruby csv2xml.rb for_xml.csv
$ ls *.xml
one.xml three.xml two.xml

Hope that helps.

James Edward Gray II

.



Relevant Pages

  • Re: opening a client application from a server?
    ... database into my script, generate many csv files, or txt files, or pdf ... files, or xml file, etc., and generate links. ... dynamically from a script (similar to what I suppose eBay, Amazon, ... incorporate a Content-Type element in the XML would create a problem. ...
    (comp.lang.perl.misc)
  • Re: dealing with max number of fields
    ... If you're going to be supplying the data as a CSV or XML file, ... You could, for example, open multiple recordsets to retrieve the data ... Expense is an issue. ...
    (microsoft.public.access.externaldata)
  • CSV to XML
    ... My first post comes as a ... request for some help. ... The CSV has 4 columns that I want to map to XML nodes. ... I am having trouble writing each CSV line to an individual XML file (100 ...
    (comp.lang.ruby)
  • Re: XML parsing using PERL.... URGENT
    ... I am new to PERL and i want to parse the xml file to csv with only 2 ... tags using perl. ...
    (comp.lang.perl.misc)
  • XML parsing using PERL.... URGENT
    ... I am new to PERL and i want to parse the xml file to csv with only 2 ... tags using perl. ...
    (comp.lang.perl.misc)