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