From: Phrogz Date: 2008-01-13T14:39:58+09:00 Subject: Re: Stream Parsing with REXML On Jan 12, 9:58 am, Marc Hoeppner wrote: > Find the element "Gene-ref", allow me to access its children and then > close and repeat for the next "Gene-ref entry. In xml code, that would > look like > > >   >   ... >   ... >   ... >   >   >  ... Here's a more targeted example, since I'm feeling helpful: class Gene attr_accessor :name, :start, :end def initialize( name=nil, start=nil, endd=nil ) @name = name self.start = start self.end = endd end def start=( start ) @start = start.to_i end def end=( endd ) @end = endd.to_i end def duration @end - @start end def to_s " " end end class GeneParser GENE_TAG_NAME = "Gene-ref" def tag_start( name, attributes ) case name when "root" # ignore when GENE_TAG_NAME @current_gene = Gene.new else @current_property = name end end def text( str ) if @current_gene && @current_property @current_gene.send( @current_property.to_s + "=", str ) end end def tag_end( name ) if name == GENE_TAG_NAME puts @current_gene @current_gene = nil else @current_property = nil end end end require 'rexml/document' REXML::Document.parse_stream( DATA, GeneParser.new ) #=> OUTPUT: #=> #=> __END__ foo 17 42 bar 43 50