ruby-on-rails - 使用 Ruby on Rails 4.0 解析本地 XML 文件

标签 ruby-on-rails ruby xml xml-parsing ruby-on-rails-4

我有一个文件夹,其中包含许多子文件夹,其中包含有关英雄联盟冠军信息的 xml 文件。我的最终目标是拥有一个列出所有英雄联盟冠军的索引页面和一个包含每个英雄详细信息的显示页面。

在学习 Rails 之前我就尝试过这样做,这就是我目前所拥有的。


File.open("champion_list.txt", "w") do |file_name|
    File.open("champions.txt", "r").each do |folder_name|
      folder_name = folder_name.strip
      xml = File.open("LoLChampions/data/#{folder_name}/champion.xml").read
      file_name.write(xml)
    end
end

逐行分割...

#open the list of every champion
#read each champion individually and do this to each one
#strips the individual champions of useless text including a ton of /n /br's
#reads the champion file inside of it's folder which is named after the name of each champion that has been grabbed from the textfile.
#each champion has a folder named the name of the champion and each champion folder contains images of the champion and a champion.xml file.

理想情况下,我想手动解析它,但我愿意使用其他现有的库。我提到过使用 JSON。

其次,每个冠军的子文件夹中都包含图像,我想知道是否需要提取每个图像并将其放入 asset/images 文件夹中。

第三,Rails 4.0 中所有冠军 xml 文件都在哪里

我知道这是一个很大的问题,但请随意回答部分问题。

谢谢, 伊恩

最佳答案

  1. 要解析 XML,我会选择 Nokogiri http://nokogiri.org/
  2. 您不一定需要在 Assets (或公共(public))下移动图像。您还可以为这些图像创建路由和操作并使用 send_file,请参阅 http://apidock.com/rails/ActionController/DataStreaming/send_file
  3. 我不确定我是否理解这部分。 Rails 当然没有冠军 xml 文件位置的约定。选择一个感觉方便的地方,并记住将其记录到自述文件中,以便其他开发人员(或 future 的您)知道在哪里可以找到它们。

关于ruby-on-rails - 使用 Ruby on Rails 4.0 解析本地 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640722/

相关文章:

ruby-on-rails - 如何在 Ruby 中解析没有年份的日期

javascript - 使用javascript从xml中删除子节点

c# - 访问具有特定值的 XElement

ruby-on-rails - Rails 未初始化常量 ActiveSupport::Autoload (NameError)?

ruby-on-rails - Sidekiq 超时

javascript - 如何更新 Rails 中 Ajax 删除的记录?

ruby-on-rails - 没有要加载的此类文件-运行 cucumber 时zip/zip(MissingSourceFile)

ruby - 在 ruby​​ 中映射哈希的更简洁方法

ruby-on-rails - my_object.save(false) 并没有真正跳过我的 Active Record 验证

java - Horizo​​ntalScrollView 和当前按钮