From: Phrogz Date: 2008-01-13T14:25:04+09:00 Subject: Re: Stream Parsing with REXML On Jan 12, 9:58 am, Marc Hoeppner wrote: > Hi (again, sort of) :) > > I am still on my quest to write a program that parses a large XML file. > After having tried to do it in tree mode, I had to realize that the > performance was simply abysmal. So back to the drawing board. But, and > here is the thing...I could find a good straight-forward tutorial on how > to write a stream parser using REXML. The official tutorial is pretty > much mute on that part and the only other example I found (or rather was > pointed to -http://www.janvereecken.com/2007/4/11/event-driven-xml-parser-in-ruby) > was way too complex for someone like me who is still pretty much a > beginner in ruby. > > So, what I am looking for is either a brief description of how to write > an event driven parser or else a link to a good and simple tutorial. xml = < I ate <raw> sausages&tm;! sausages&tm;! ]]> whoa! ENDXML class Listener def initialize # Use instance variables to mantain information # between different callbacks. @nest_level = 0 end def tag_start( name, attr_hash ) @nest_level += 1 print " " * @nest_level puts "<#{name} ...> #{attr_hash.inspect}" end def text( str ) print " " * @nest_level p str end # Treat CDATA sections just like text alias_method :cdata, :text def tag_end( name ) print " " * @nest_level puts "" @nest_level -= 1 end end require 'rexml/document' require 'stringio' # for this demo stream_handler = Listener.new pseudo_file = StringIO.new( xml ) REXML::Document.parse_stream( pseudo_file, stream_handler ) #=> OUTPUT: #=> "oh hai\n" #=> {} #=> "\n " #=> {"jim"=>"jam", "foo"=>"bar"} #=> #=> "\n " #=> {"foo"=>"sausage"} #=> "\n " #=> {} #=> "I ate sausages&tm;!" #=> #=> "\n " #=> {} #=> "\n " #=> "\n I ate sausages&tm;!\n " #=> "\n " #=> #=> "\n " #=> #=> "\n" #=> #=> "\nwhoa!\n"