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 and .

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:




Programm Name




Shop 1
45001

Chris, the SAX Parser has an “on_characters” callback. You need to note
when you see an on_start_tag for the tag “row”. Then, in your
on_characters callback, you use the characters it presents you. When
you see an end tag for “row”, you need to turn off your note. Get it?

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