XML to CSV

S

Stuart Clarke

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
 
D

David Masover

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top