sax parsing

S

Shweta Kadam

hi,
I'm using sax parsing in my code but confused about how to add
listener's to it and can you tell me in detail how to write sax code in
ruby as well as listener's.
 
R

Robert Dober

On Wed, May 7, 2008 at 1:07 PM, Shweta Kadam
#
# There are more ways, but I like most to use blocks
# If you prefer to implement listeners as objects the documentation below
# should give you some sound hints too, I just never felt the need for it.
#
require 'rexml/parsers/sax2parser'

parser = REXML::parsers::SAX2Parser.new( DATA.read )

parser.listen :start_element do
|uri, locname, qname, atts|

puts "I am in start_element and was called with " <<
[uri, locname, qname, atts].inspect
end

parser.listen :characters do
| text |
puts %<I am in characters and was called with "#{text}">
end
#
# For all type of listeners see here:
# http://www.ruby-doc.org/core/classes/REXML/Parsers/SAX2Parser.html#M005185
# and for the paramters they take have a look at the corresponding method here:
# http://www.ruby-doc.org/core/classes/REXML/SAX2Listener.html
parser.parse

__END__
<root>
<node1>
<node2 att1="www">
content
</node2>
</node1>
</root>


HTH
Robert
 

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
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top