XML to JSON API Proxy for Quick Mash-ups

While building our company dashboard, which was mentioned in our previous blog post on SVG with Raphael, I came across a problem with the project management tool we use, Pivotal Tracker. 

The Pivotal Tracker API is only in XML, which means you can't do cross-domain requests and access the API via javascript. This meant that, if I wanted to extract data about the status of our user stories, I would have to do it on the server side. As everything so far had been done with javascript on the client side, I was reluctant to start making things more complicated.

I contacted Pivotal, who said they might think about adding JSON at some point, but I didn't want to wait for them to sort it out. Instead, I built a quick API proxy that allows you to make a request to a XML API and get back JSON. So, now, I can do this in my javascript:

It will proxy the GET request to the API and convert the XML response into JSON, via http://xml2json.heroku.com (supports JSONP). I've put the code on GitHub http://github.com/bitzesty/xml2jsonp in case anyone else needs to do the same thing.

Filed under  //  API   javascript   open source   xml  
Comments (6)
Posted by Matthew Ford 

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 covers 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 and 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 http://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.

Gnip-cpu

Filed under  //  ruby on rails   xml  
Comments (0)
Posted by Matthew Ford