XML to CSV

I am struggling to see how I would go about extracting data from an XML
file like so:

<?xml version="1.0" encoding="UTF-8" ?>
  • 00000
  • 100000000
  • THIS IS A NEW RECORD
  • 00000
  • 100000000

My code thus far is

require ‘rexml/document’
xml = REXML::Document.new(File.open(“H:\test.xml”))
csv_file = File.new(“H:\data.csv”, “w”)
xml.elements.each("//event") do |e|
csv_file.puts e.attributes[‘id’] << “|” <<
e.elements[‘object class=“com.########l”’].text << “|” <<
e.elements[‘void property="###########"/long’].text << “|” <<
e.elements[‘void property=“description”/string’].text << “|” <<
e.elements[‘void property=“downloaded”/long’].text
end

This is however failing.

Any ideas?

Many thanks

On Thursday 08 April 2010 01:25:05 pm Stuart C. wrote:

My code thus far is

require ‘rexml/document’

REXML probably isn’t the best place to start.

This is however failing.

That’s like saying “My computer broke” – it tells us absolutely
nothing.

How is it failing? What happens when you try to do that? Does it raise
an
exception? Does it create a CSV file that doesn’t work? Does it empty
your
bank accounts and go to Vegas?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs