# CSV to XML

**URL:** <https://www.ruby-forum.com/t/csv-to-xml/151793>\
**Category:** Ruby\
**Created:** [October 25, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/csv-to-xml/151793 "2008-10-25T20:41:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [October 25, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/csv-to-xml/151793/1 "2008-10-25T20:41:34Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [October 26, 2008, 12:11am UTC](https://www.ruby-forum.com/t/csv-to-xml/151793/2 "2008-10-26T00:11:08Z")

</div>

Thanks James, gettign this error, trying to work it out:

makeXML.rb:7: undefined method `+' for nil:NilClass (NoMethodError) from /Library/Ruby/Gems/1.8/gems/fastercsv-1.4.0/lib/faster_csv.rb:1514:in`each’  
from  
/Library/Ruby/Gems/1.8/gems/fastercsv-1.4.0/lib/faster\_csv.rb:1017:in  
`foreach' from /Library/Ruby/Gems/1.8/gems/fastercsv-1.4.0/lib/faster_csv.rb:1191:in`open’  
from  
/Library/Ruby/Gems/1.8/gems/fastercsv-1.4.0/lib/faster\_csv.rb:1016:in  
`foreach’  
from makeXML.rb:6

---

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [October 25, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/csv-to-xml/151793/3 "2008-10-25T21:33:10Z")

</div>

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

> (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 G. II
