Tuesday, January 25, 2011

[Rails] Help with libxml-ruby and SAX parser

Hello,

I try to read through an XML document with the libxml-ruby SAX parser.
It is possible to execute the parser and I can see the output of the
element according to the "puts" in the Saxparser class.

However I do not know how to get the results after the
"on_start_element" and write them in to a hash. I'm browsing through
Google since 3 days without any luck to find an example or
understandable documentation how to get the data.

I need to get the data between <row> and </row>.

Is there anybody that can help me?

Thanks


I have done following:

Created a saxparser.rb model that looks like this:
-----------------------------------------------------------------
require 'rubygems'
require 'xml/libxml'

class Saxparser
include XML::SaxParser::Callbacks

def on_start_element(element, attributes)
if element == 'row'
puts element
end
end
end

Created method to execute the sax parser:
-----------------------------------------------------------------
def self.vbdownload_td_xml
require 'net/http'

# download the xml file
download_td_xml

parser = XML::SaxParser.file("#{RAILS_ROOT}/tmp/td.xml")
parser.callbacks = Saxparser.new
parser.parse
end

The XML itself looks like this:
-----------------------------------------------------------------
<report title="Event breakdown report"
name="aAffiliateEventBreakdownReport" time="2011-01-25 22:14">
<matrix rowcount="310">
<columns>
<programName type="string">Programm Name</programName>
...
</columns>
<rows>
<row>
<programName>Shop 1</programName>
<programId>45001</programId>
...

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate