# XML to CSV

**URL:** <https://www.ruby-forum.com/t/xml-to-csv/186553>\
**Category:** Ruby\
**Created:** [April 8, 2010, 8:25pm UTC](https://www.ruby-forum.com/t/xml-to-csv/186553 "2010-04-08T20:25:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [April 8, 2010, 8:25pm UTC](https://www.ruby-forum.com/t/xml-to-csv/186553/1 "2010-04-08T20:25:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [April 11, 2010, 2:29am UTC](https://www.ruby-forum.com/t/xml-to-csv/186553/2 "2010-04-11T02:29:15Z")

</div>

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?
