# Rexml - please help

**URL:** <https://www.ruby-forum.com/t/rexml-please-help/122734>\
**Category:** Rails\
**Created:** [December 11, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/rexml-please-help/122734 "2007-12-11T13:46:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [December 11, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/rexml-please-help/122734/1 "2007-12-11T13:46:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [December 11, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/rexml-please-help/122734/2 "2007-12-11T15:31:01Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [December 11, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/rexml-please-help/122734/3 "2007-12-11T15:49:25Z")

</div>

and by the way, if you want to parse XML, then hpricot may be worth a  
try:  
[http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/)

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [December 11, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/rexml-please-help/122734/4 "2007-12-11T16:46:13Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [December 12, 2007, 4:54am UTC](https://www.ruby-forum.com/t/rexml-please-help/122734/5 "2007-12-12T04:54:25Z")

</div>

Hi Guys,

thank you for both Dejan and Thorsten

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

cheers  
sameera

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [December 12, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/rexml-please-help/122734/6 "2007-12-12T20:32:50Z")

</div>

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
