How to cache an xml file and read its attributes in Rails?
I have a file that represents all components of a game. I want to l开发者_JAVA技巧oad the file into cache on boot, then have the ability to call the attributes of the file from the controllers. How do I begin?
I like http://cobravsmongoose.rubyforge.org for most simple XML handling:
CobraVsMongoose.xml_to_hash(File.open('path/to/xml').gets)
As for your specific case, I would add an initializer which requires cvm and sets the value above to a constant, which you could then access wherever you want...
# config/initializers/load_xml.rb
require 'cobravsmongoose'
MY_XML = CobraVsMongoose.xml_to_hash(File.open('path/to/xml').gets)
Try out REXML, its an XML parsing library for Ruby. I think it comes with the standard version of Ruby, so you shouldn't even need to install a gem.
精彩评论