Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将XML解析为ruby对象并保存_Ruby_Xml_Parsing_Mongoid - Fatal编程技术网

将XML解析为ruby对象并保存

将XML解析为ruby对象并保存,ruby,xml,parsing,mongoid,Ruby,Xml,Parsing,Mongoid,我使用happy mapper在ruby中进行对象映射,然后解析从api获得的.xml文件中的xml数据 我在api响应中得到一个zip文件,并将其解压缩,得到具有相同xml数据格式的5-6个文件。 每个文件中的数据约为2-3MB 我想将这些数据保存在文件中,记住我应该能够对其执行搜索操作。 我不想使用关系数据库,而是希望将数据保存在文件中。 保存数据的更好方法应该是什么?该方法将足够有效,以便以后对该数据执行搜索操作 require 'json' require 'happymapper'

我使用happy mapper在ruby中进行对象映射,然后解析从api获得的.xml文件中的xml数据

我在api响应中得到一个zip文件,并将其解压缩,得到具有相同xml数据格式的5-6个文件。 每个文件中的数据约为2-3MB

我想将这些数据保存在文件中,记住我应该能够对其执行搜索操作。 我不想使用关系数据库,而是希望将数据保存在文件中。 保存数据的更好方法应该是什么?该方法将足够有效,以便以后对该数据执行搜索操作

require 'json'
require 'happymapper'

file_contents = File.read('/home/GhostRider/x.xml')    



  class Message
    include HappyMapper

    tag 'Message'
    element :color, String, :tag => 'Colour'
    element :bg_color, String, :tag => 'BgColour'

  end


  class Status
    include HappyMapper

    tag 'Status'
    element :text, String, :tag => 'Text'
    element :color, String, :tag => 'Colour'
    element :bg_color, String, :tag => 'BgColour'

    has_one :message, Message

  end

  class Line
    include HappyMapper

    tag 'Line' # if you put class in module you need tag
    element :name, String, :tag => 'Name'
    element :color, String, :tag => 'Colour'
    element :bg_color, String, :tag => 'BgColour'
    element :url, String, :tag => 'Url'

    has_one :status, Status

  end

  class Lines
    include HappyMapper

    tag 'Lines' # if you put class in module you need tag

    has_many :lines, Line
  end


item = Lines.parse(file_contents, :single => true)

item.lines.each do |i|

  puts i.name, i.color, i.url, i.status.text, i.status.message.color
end

我需要保存获取的数据。

更好的方法是使用xml选择器(如nokogiri或xml simple或默认哈希)从rails解析xml(xml文件)。然后分别定义ruby类,这有助于更好地控制类和执行操作。

那么,问题是什么?@SergioTulentsev检查更新,missed dat:(