class GenericFormatter < Formatter
attr_accessor :tag_name,:objects
def generate_xml
builder = Nokogiri::XML::Builder.new do |xml|
xml.send(tag_name.pluralize) {
objects.each do |obj|
xml.send(tag_name.singularize){
self.generate_obj_row obj,xml
}
end
}
end
builder.to_xml
end
def initialize tag_name,objects
self.tag_name = tag_name
self.objects = objects
end
def generate_obj_row obj,xml
obj.attributes.except("updated_at").map do |key,value|
xml.send(key, value)
end
xml.updated_at obj.updated_at.try(:strftime,"%m/%d/%Y %H:%M:%S") if obj.attributes.key?('updated_at')
end
end
In the above code, I implemented a formatter where I used the nokogiri XML-Builder to generate XML by manipulating objects that pass through the code. It generates faster XML, when the data is not too large, if the data is more than more than 10,000 records, then it slows down the creation of XML and takes at least 50-60 seconds.
Problem: Is there a way to generate XML faster, I also tried XML Builders, but it didn’t work. How can I generate XML faster? Should the solution be an application on rails 3 and suggestions for optimizing the code above?
source
share