Robert Klemme
3/17/2009 3:10:00 PM
On 17.03.2009 13:03, Sandy Thomson wrote:
> 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::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
Here you need to iterate through all the "performance" elements _below
the current event_ and concatenate the individual performance's date
with what you have built so far.
You should probably also take measures to emit a line without a date in
case zero performances can be found in input XML.
> 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!
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end