Boris Glawe wrote:
> Hi,
>
> I am trying to parse an xml file on my fedora Core 3 machine.
> I started with a tutorial from this site:
> http://www.germane-software.com/software/rexml/docs/tutorial.html


If all you need is minimal xml parsing, try this:

.. class String
..   def get_attr
..     s = self
..     h = Hash.new
..     while s =~ /(\w+)="([^"]*)"/m
..       h[ $1 ] = $2
..       s = $'
..     end
..     h
..   end
..
..   def span( tagname )
..     s = self
..     while (s =~ Regexp.new(
..         '(<'+tagname+'.*?>)(.*?)</'+tagname+'>',
..         Regexp::MULTILINE))
..       yield( $1, $2 )
..       s = $'
..     end
..   end
.. end
..
.. s = ''
.. $<.each_line {|x| s=s+x}
.. s.span('section') { |tag,text|
..   puts "Section: " + tag.get_attr['name']
..   text.span('item') { |tag,text|
..     a = tag.get_attr
..     puts "  Item: upc=#{a['upc']} stock=#{a['stock']}"
..     text.span('name') { |tag,name|
..       puts "    Name: " + name
..     }
..     text.span('price') { |tag,price|
..       puts "    Price: " + price
..     }
..   }
.. }


With this input:

<inventory title="OmniCorp Store #45x10^3">
<section name="health">
<item upc="123456789" stock="12">
<name>Invisibility Cream</name>
<price>14.50</price>
<description>Makes you invisible</description>
</item>
<item upc="445322344" stock="18">
<name>Levitation Salve</name>
<price>23.99</price>
<description>Levitate yourself for up to 3 hours per
application</description>
</item>
</section>
<section name="food">
<item upc="485672034" stock="653">
<name>Blork and Freen Instameal</name>
<price>4.95</price>
<description>A tasty meal in a tablet; just add
water</description>
</item>
<item upc="132957764" stock="44">
<name>Grob winglets</name>
<price>3.56</price>
<description>Tender winglets of Grob. Just add
water</description>
</item>
</section>
</inventory>

the output is


.. Section: health
..   Item: upc=123456789 stock=12
..     Name: Invisibility Cream
..     Price: 14.50
..   Item: upc=445322344 stock=18
..     Name: Levitation Salve
..     Price: 23.99
.. Section: food
..   Item: upc=485672034 stock=653
..     Name: Blork and Freen Instameal
..     Price: 4.95
..   Item: upc=132957764 stock=44
..     Name: Grob winglets
..     Price: 3.56