Sean Russell <ser / germane-software.com> writes:

> Bob Hutchison wrote:
> 
> > There are some clever java parsers doing this kind of thing too (I think
> > the first XML parser did something along these lines, but I'm not
> > certain). But this isn't what I was getting at. Again, it is only my
> > opinion, but an event based parser should be a very high priority for
> > Ruby. Without it certain things cannot be done (or rather done remotely
> > efficiently). Specifically, the tool I'm working on (similar to an XML
> > data binding) would be seriously compromised.
> 
> AFAIK, all of the XML parsers available have event-based parsing.  NQXML 
> allows you to pass a block which is called, in which you can do a 
> case...end statement to handle the event.  REXML requires you to pass it a 
> listener, on which event methods are called (start_tag(), 
> processing_instruction(), etc.).  XMLParser has a similar mechanism.  Just 
> FYI.

See NQXML::Dispatcher by David Alan Black, distributed with NAXML, for a
very nice callback mechanism. Essentially, you associate blocks with
(possibly nested) tag names or element types. The blocks are executed when
the appropriate tags are seen. For example (from memory; not tested):

    nd = NQXML::Dispatcher.new(file)
    nd.handle(:start_element, %w(root level1 level2)) { | e |
	# do something with e
    }
    nd.handle(:text, %w(root level1 level2 level3)) { | e |
	# reads text inside <level3> tag
    }
    nd.start()

Jim
-- 
Jim Menard, jimm / io.com, http://www.io.com/~jimm/
"No, no, you're not thinking; you're just being logical." -- Niels Bohr