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" ?>
- <java version="1.6.0_17" class="java.beans.XMLDecoder">
- <object class="com.########l">
- <void method="###">
- <object class="###########">
- <void property="###########">
  <long>00000</long>
  </void>
- <void property="description">
  <string />
  </void>
- <void property="downloaded">
  <long>100000000</long>
  </void>
- <void method="###"> THIS IS A NEW RECORD
- <object class="###########">
- <void property="###########">
  <long>00000</long>
  </void>
- <void property="description">
  <string />
  </void>
- <void property="downloaded">
  <long>100000000</long>
  </void>

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

···

--
Posted via http://www.ruby-forum.com/.

My code thus far is

require 'rexml/document'

REXML probably isn't the best place to start.

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.

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?

···

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