· · Matthew Ford · 1 min read
Better XML Parsing with Rails

We recently built a Ruby on Rails application that was receiving a large amount of XML. This as you can imagine was pretty CPU intensive, especially as Rails (ActiveSupport) uses REXML under the bonnet to parse the incoming requests.
I started to wonder if it was possible to use a different XML parsing library, for an overview of some of the available libraries and benchmarks see John Nunemaker’s post about parsing xml with ruby and the rubyinside post: ruby xml performance benchmarks.
I’ve used most of these libraries at some point or another for parsing XML but I was really impressed with Nokogiri, and I desperately wanted to use it in production for the Rails backed parsing. So I started to look at the Rails source and I noticed this commit
https://github.com/rails/rails/commit/694998ee4fb8d257ba78424cab630846327a0889.
YES! All you have to do (besides installing Nokogiri on your server) is specify:
ActiveSupport::XmlMini.backend = 'Nokogiri'
In your production.rb environment and your done. The results speak for themselves, here is a snapshot of our CPU usage before and after, with the same load:

Discuss your software, technical leadership or AI plans
Tell us what you need to build, improve or decide.
We can help with software development and ongoing support, fractional CTO leadership, and practical AI adoption. Speak to our senior team about your priorities, our approach and availability.
Choose 15 or 30 minutes. Booking requests require confirmation by email.


