Rexml - please help

Hi ppl,

I’m new to this forum and also to ruby. My question is I have a rails
project and i want to load some data from an xml file and to write it.

for that i have wrote a
1 model (Oxml)
2 controller (XmlTestController)

my code is as follows,

---------------- model -----------------------------

require ‘rexml/document’
class Oxml
def read_xml()
xml = REXML::Document.new(File.open(’/images/guitars.xml’))
end
end

------------- controller ---------------------------

class XmlTestController < ApplicationController
def index
@oxm = Oxml.new
@oxm.read_xml
end
end

and i’m getting this error,

Errno::ENOENT in Xml testController#index

No such file or directory - /images/guitars.xml

can somebody tell a solution for this. And if i’m wrong pls tell me how
to correct the code.

any help would be really appriciated… and thankx in advance

cheers
sameera

The file name that you try to open is not a valid one, as the error
describe to you.
You should give a valid path to the desired file.
Since it is a Rails application you should build the path in this
fashion:

class Oxml
def read_xml()
xml = REXML::Document.new(File.open("#{RAILS_ROOT}/images/
guitars.xml"))
end
end

On Dec 11, 1:46 pm, Sameera G. [email protected]

and by the way, if you want to parse XML, then hpricot may be worth a
try:

Hpricot is, from my experience, much faster for reading, querying and
manipulating xml files.
You definitely should give it a try.

On Dec 11, 3:49 pm, Thorsten M. <rails-mailing-l…@andreas-

Hi Guys,

thank you for both Dejan and Thorsten

I’ll give a try and see. Thankx again for the help

cheers
sameera

Try:

xml_file = File.join(RAILS_ROOT,‘images/guitars.xml’)
File.open(xml_file)

(There’s no actual need for the temp variable, I just thought it
looked clearer)

‘/images/guitars.xml’ tells file open to look in ‘/images’ (as in,
anchored to the root directory for the system) for guitars.xml.
File.join( ) makes a full path out of the arguments you give it.
RAILS_ROOT is a constant set by rails as the root directory of the
project.

Regards
jon

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs