S
Sandy Thomson
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 id='1234'>
<title>Event Title</title>
<category>Event Category</category>
<venue>
<name>Venue Name</name>
<address>
<address1>1 Some Street</address1>
<town>Some Town</town>
</address>
</venue>
<performances>
<performance date='2009-04-01 18:00:00' />
<performance date='2009-04-03 18:00:00' />
</performances>
</event>
This is my extraction code:
require 'rexml/document'
xml = REXML:
ocument.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!
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 id='1234'>
<title>Event Title</title>
<category>Event Category</category>
<venue>
<name>Venue Name</name>
<address>
<address1>1 Some Street</address1>
<town>Some Town</town>
</address>
</venue>
<performances>
<performance date='2009-04-01 18:00:00' />
<performance date='2009-04-03 18:00:00' />
</performances>
</event>
This is my extraction code:
require 'rexml/document'
xml = REXML:
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!