CSV to XML

B

Bee Tard

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!
 
J

James Gray

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top