2008/1/14, Marc Hoeppner <marc.hoeppner / molbio.su.se>:
> Ok, I am still on this and am starting to feel a bit stupid...I guess
> there is something wrong with the following example, but maybe I should
> step back for a while. Anyhow - what's missing to make it work:
>
> The xml file:
>
> <Gene-ref>
>   <name>Gene1</name>
>   <start>1</start>
>   <end>10</end>
>  </Gen-ref>
>  <Gene-ref>
>   <name>Gene2</name>
>   <start>11</start>
>   <end>20</end>
>  </Gen-ref>
>  <Gene-ref>
>   <name>Gene3</name>
>   <start>21</start>
>   <end>30</end>
>  </Gen-ref>
>  <Gene-ref>
>   <name>Gene4</name>
>   <start>31</start>
>   <end>40</end>
>  </Gen-ref>
>  <Gene-ref>
>   <name>Gene5</name>
>   <start>41</start>
>   <end>50</end>
>  </Gen-ref>
>
> The code:
>
> data = File.new(ARGV.shift)
>
> class Listener
>
>   def tag_start( name, attr_hash )
>     if name == 'Gene-ref'
>      @name = name
>   end
>   end
>
>   def text( str )

You need to place the @name storing code here.  But to make it work
you need to remember the current tag's name because you want to only
store "str" in @name if you are in <name>.  This makes it necessary
that you add a stack to your listener class which gets pushed and
popped whenever tag_start and tag_end are invoked.

>   end
>
>   def tag_end( name )
>     if name == 'Gene-ref'
>       puts @name
>       @name = nil
>     end
>   end
> end
>
>
> require 'rexml/document'
> REXML::Document.parse_stream( data, Listener.new )

Cheers

robert

-- 
use.inject do |as, often| as.you_can - without end