From: Sandy Thomson Date: 2009-03-17T21:03:09+09:00 Subject: XML to CSV with REXML - I'm sure this should be easy... Ok, I'm taking a fairly simple xml file containing a series of events and I want to convert it to csv - nothing new there. However, some events have two or more dates listed and I'd like to display each as individual lines. My ruby skills are fairly limited but from googling around I can extract everything up to the dates, but I'm banging my head against a wall to get any further... Here's the XML: Event Title Event Category Venue Name
1 Some Street Some Town
This is my extraction code: require 'rexml/document' xml = REXML::Document.new(File.open("data.xml")) csv_file = File.new("data.csv", "w") xml.elements.each("//event") do |e| csv_file.puts e.attributes['id'] << "|" << e.elements['title'].text << "|" << e.elements['category'].text << "|" << e.elements['venue/name'].text << "|" << e.elements['venue/address/address1'].text << "|" << e.elements['venue/address/town'].text end Which gives me: 1234|Event Title|Event Category|Venue Name|1 Some Street|Some Town But what I really want is: 1234|Event Title|Event Category|Venue Name|1 Some Street|Some Town|2009-04-01 18:00:00 1234|Event Title|Event Category|Venue Name|1 Some Street|Some Town|2009-04-03 18:00:00 I'm sure this should be fairly simple but any help would be appreciated. Cheers! -- Posted via http://www.ruby-forum.com/.