I’m using happy mapper for object mapping in ruby , then parse the xml data in the .xml files i obtain from the api.
I get a zip file in the api response and extract it to get 5-6 files with same xml format of data.
The data in each file is around 2-3 mb.
I want to save this data in files keeping in mind that i should be able to perform search operations over it.
I don’t want to use relational db rather would be looking to save the data in files.
What should be the better approach to save the data which will be efficient enough for the later search operations to be performed on that data.
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
I need to save this data obtained.
The better approach is to parse the xml using xml selectors like nokogiri or xml simple or default Hash.to_xml(xml file) from rails. Then define the ruby classes separately which helps in keeping better control over the classes and perform the operations.