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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 Gayan
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> 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
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
and by the way, if you want to parse XML, then hpricot may be worth a try: http://code.whytheluckystiff.net/hpricot/ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Mueller <rails-mailing-l...@andreas- s.net> wrote:> and by the way, if you want to parse XML, then hpricot may be worth a > try:http://code.whytheluckystiff.net/hpricot/ > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Guys, thank you for both Dejan and Thorsten I''ll give a try and see. Thankx again for the help cheers sameera -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---