[REXML] is my installation not working?

B

Boris Glawe

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

#!/usr/bin/ruby

require "rexml/document"
file = File.new( "mydoc.xml" )
doc = REXML::Document.new file

When executing this trivial peace of code I get the message:

/usr/lib/ruby/1.8/rexml/element.rb:2:in `require': No such file to load --
rexml/namespace (LoadError)
from /usr/lib/ruby/1.8/rexml/element.rb:2
from /usr/lib/ruby/1.8/rexml/document.rb:1:in `require'
from /usr/lib/ruby/1.8/rexml/document.rb:1
from ./test.rb:3:in `require'
from ./test.rb:3

What's the problem? Is something wrong in the code or is my installation missing
this "rexml/namespace" file?

This for any hint.

greets Boris
 
Z

Zach Dennis

Boris said:
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

#!/usr/bin/ruby

require "rexml/document"
file = File.new( "mydoc.xml" )
doc = REXML::Document.new file

When executing this trivial peace of code I get the message:

/usr/lib/ruby/1.8/rexml/element.rb:2:in `require': No such file to load
-- rexml/namespace (LoadError)
from /usr/lib/ruby/1.8/rexml/element.rb:2
from /usr/lib/ruby/1.8/rexml/document.rb:1:in `require'
from /usr/lib/ruby/1.8/rexml/document.rb:1
from ./test.rb:3:in `require'
from ./test.rb:3

What's the problem? Is something wrong in the code or is my installation
missing this "rexml/namespace" file?

This for any hint.

The xml file "mydoc.xml" has to exist, for this tutorial to work. It
doesn't unless you created it, last time I checked. A few more lines
down he gives a xml example of using a String, copy and paste that into
a file and call if "mydoc.xml". Let us know if it don't go... happy rubying,

Zach
 
B

Boris Glawe

The xml file "mydoc.xml" has to exist, for this tutorial to work. It
doesn't unless you created it, last time I checked. A few more lines
down he gives a xml example of using a String, copy and paste that into
a file and call if "mydoc.xml". Let us know if it don't go... happy
rubying,

Zach

The file exists and is a valid xml file according to the firefox DOM expector.

I've scanned my whole disk: there is no file called "namespace.rb" on the whole
system.

The strange thing is, that the required file has been part of the ruby-libs
package before the last upgrade. But after having upgraded to the newest ruby
version, the file was not there any more (The upgrade was provided by fedora).

Would you agree, that the installation/package is broken?

Thanks

Boris
 
N

Nicholas Van Weerdenburg

Did you check for the namespace file? I have a namespace.rb in my
rexml directory.

Nick
 
N

Nicholas Van Weerdenburg

Sure sounds like it. Maybe the maintainer disagreed with XML's
namespace implementation :). I've heard that it was a fairly heated
process.
 
W

William James

Boris said:
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
 
W

William James

Boris said:
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
 
B

Boris Glawe

I use Fedora Core 3 and file indeed is missing.
Cheers, Jacek Balcerski

The file has been put into the wrong package according to the package maintainer.

Install the ruby-tcltk package and it will work again.

greets Boris
 
W

William James

Previous version was too bloated.

.. class String
.. def span( tagname )
.. self.scan( Regexp.new(
.. '(<'+tagname+'.*?>)(.*?)</'+tagname+'>',
.. Regexp::MULTILINE ) ) { |a,b| yield a,b }
.. end
.. def get_attr
.. h = Hash.new
.. self.scan( /(\w+)="([^"]*)"/m ).each { |a,b|
.. h[ a ] = b }
.. h
.. 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
.. }
.. }
.. }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top