Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6585625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:41:13+00:00 2026-05-25T16:41:13+00:00

I want to write approximately 50MB of data to an XML file. I found

  • 0

I want to write approximately 50MB of data to an XML file.

I found Nokogiri (1.5.0) to be efficient for parsing when just reading and not writing. Nokogiri is not a good option to write to an XML file since it holds the complete XML data in memory until it finally writes it.

I found Builder (3.0.0) to be a good option but I’m not sure if it’s the best option.

I tried some benchmarks with the following simple code:

  (1..500000).each do |k|
    xml.products {
      xml.widget {
        xml.id_ k
        xml.name "Awesome widget"
      }
    }
    end

Nokogiri takes about 143 seconds and also memory consumption gradually increased and ended at about 700 MB.

Builder took about 123 seconds and memory consumption was stable enough at 10 MB.

So is there a better solution to write huge XML files (50 MB) in Ruby?

Here’s the code using Nokogiri:

require 'rubygems'
require 'nokogiri'
a = Time.now
builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    (1..500000).each do |k|
    xml.products {
      xml.widget {
        xml.id_ k
        xml.name "Awesome widget"
      }
    }
    end
  }
end
o = File.new("test_noko.xml", "w")
o.write(builder.to_xml)
o.close
puts (Time.now-a).to_s

Here’s the code using Builder:

require 'rubygems'
require 'builder'
a = Time.now
File.open("test.xml", 'w') {|f|
xml = Builder::XmlMarkup.new(:target => f, :indent => 1)

  (1..500000).each do |k|
    xml.products {
      xml.widget {
        xml.id_ k
        xml.name "Awesome widget"
      }
    }
    end

}
puts (Time.now-a).to_s
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:41:14+00:00Added an answer on May 25, 2026 at 4:41 pm

    Solution 1

    If speed is your main concern, I’d just use libxml-ruby directly:

    $ time ruby test.rb 
    
    real    0m7.352s
    user    0m5.867s
    sys     0m0.921s
    

    The API is pretty straight forward:

    require 'rubygems'
    require 'xml'
    doc = XML::Document.new()
    doc.root = XML::Node.new('root_node')
    root = doc.root
    
    500000.times do |k|
      root << elem1 = XML::Node.new('products')
      elem1 << elem2 = XML::Node.new('widget')
      elem2['id'] = k.to_s
      elem2['name'] = 'Awesome widget'
    end
    
    doc.save('foo.xml', :indent => false, :encoding => XML::Encoding::UTF_8)
    

    Using :indent => true doesn’t make much difference in this case, but for more complex XML files it might.

    $ time ruby test.rb #(with indent)
    
    real    0m7.395s
    user    0m6.050s
    sys     0m0.847s
    

    Solution 2

    Of course the fastest solution, and that doesn’t build up on memory is just to write the XML manually but that will easily generate other sources of error like possibly invalid XML:

    $ time ruby test.rb 
    
    real    0m1.131s
    user    0m0.873s
    sys     0m0.126s
    

    Here’s the code:

    f = File.open("foo.xml", "w")
    f.puts('<doc>')
    500000.times do |k|
      f.puts "<product><widget id=\"#{k}\" name=\"Awesome widget\" /></product>"
    end
    f.puts('</doc>')
    f.close
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want write a simple query which will fetch data from a table (which
I want to write <a href=product.php?id=5> as product/5.I am not talking about user to
I want to write a file on my iPhone app. My file is in
I have a large data file, which is zipped, and approximately 20MB. When it's
HellO, I want to write a binary file format viewer for windows which can
I have not found it anywhere so I am asking. How can I write
I want write an win-form application to convert data from foxpro table into sql
i want write a mapping File for following Class, but i dont know how.
I am using spring 3.0 org.springframework.web.multipart.commons.CommonsMultipartFile for file uploading. i want write down unit
I'm outputting pretty huge XML structure to file and I want user to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.