Slower when creating XML from a group of model objects

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?

+6
source share
2

, , , . , ActiveRecord, XML-. - ( send).

:

class XmlGenerator
  attr_accessor :tag_name, :ar_relation

  def initialize(tag_name, ar_relation)
    @ar_relation = ar_relation
    @tag_name = tag_name
  end

  def generate_xml
    singular_tag_name = tag_name.singularize
    plural_tag_name = tag_name.pluralize

    xml = ""
    xml << "<#{plural_tag_name}>"

    ar_relation.find_in_batches(batch_size: 1000) do |batch|
      batch.each do |obj|
        xml << "<#{singular_tag_name}>"

        obj.attributes.except("updated_at").each do |key, value|
          xml << "<#{key}>#{value}</#{key}>"
        end

        if obj.attributes.key?("updated_at")
          xml << "<updated_at>#{obj.updated_at.strftime('%m/%d/%Y %H:%M:%S')}</updated_at>"
        end

        xml << "</#{singular_tag_name}>"
      end
    end

    xml << "</#{tag_name.pluralize}>"
    xml
  end
end

# example usage
XmlGenerator.new("user", User.where("age < 21")).generate_xml

:

  • , ActiveRecord ActiveRecord.
  • xml , xml, , builder

60 . . XML- 40 .

, , , .

:

  • ActiveRecord , .
  • , .
  • xml ( )
+3

Nokogiri gem XML , Nokogiri - libxml2.

Gemfile gem 'nokogiri' XML, XML-Builder Nokogiri,

xml = Nokogiri::XML::Builder.new { |xml| 
    xml.body do
        xml.test1 "some string"
        xml.test2 890
        xml.test3 do
            xml.test3_1 "some string"
        end
        xml.test4 "with attributes", :attribute => "some attribute"
        xml.closing
    end
}.to_xml

<?xml version="1.0"?>
<body>
  <test1>some string</test1>
  <test2>890</test2>
  <test3>
    <test3_1>some string</test3_1>
  </test3>
  <test4 attribute="some attribute">with attributes</test4>
  <closing/>
</body>

: http://www.jakobbeyer.de/xml-with-nokogiri

+1

All Articles