multi_xml: Flexible and fast XML parsing in Ruby
An overlooked part of writing a good API wrapper is picking the right parsing library. It’s often a choice between speed and complex platform dependencies. Gems like MultiJSON from Intridea are valuable in providing a common interface to swappable JSON parsers, letting users of your library choose the best fit for their application.
Erik Michaels-Ober took inspiration from MultiJSON and has released MultiXML which aims to bring the swappable parsing approach to XML documents. It looks for the “best” library to use based on parsing speed, looking first for LibXML, then Nokogiri, then Hpricot, and finally REXML. Of course, you can specify your own library of choice.
You can install MultXML via RubyGems:
gem install multi_xml
Then just require
it and parse some XML:
require 'multi_xml'
MultiXml.parse('<tag>This is the contents</tag>') # parsed using LibXML if you got it
Or we can specify our library:
MultiXml.engine = :nokogiri
MultiXml.parse('<tag>This is the contents</tag>') # parsed using Nokogiri if you got it
You can set the engine
via symbol or class:
MultiXml.engine = :rexml
# or
MultiXML.engine = MultiJson::Engines::Rexml
MultiXML was just released this morning so if you have ideas to add, fork away.
Discussion
Sign in or Join to comment or subscribe