【发布时间】:2012-06-27 20:03:31
【问题描述】:
我在 ruby 中使用快乐映射器进行对象映射,然后解析我从 api 获得的 .xml 文件中的 xml 数据。
我在 api 响应中得到一个 zip 文件并将其解压缩以获取 5-6 个具有相同 xml 数据格式的文件。 每个文件中的数据约为 2-3 mb。
我想将这些数据保存在文件中,记住我应该能够对其执行搜索操作。 我不想使用关系数据库,而是希望将数据保存在文件中。 保存数据的更好方法应该是什么,该方法对于以后对该数据执行的搜索操作足够有效。
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
我需要保存获得的这些数据。
【问题讨论】:
-
那么,问题是什么?
-
@SergioTulentsev 检查更新,错过了 :(