REXML: Proccessing XML in Ruby
In this day and age of software development, it is inevitable that you will need to process XML or produce XML within your application. If your language of choice is Ruby, or Rails for that matter, there is a very simple and useful XML processing API for Ruby called REXML. REXML is a pure Ruby XML processor with an easy to use API. This article will introduce REXML, and show you how to use it to do some common XML processing tasks.
Ruby: The Scripting Language that is Taking the Computing World by Storm
It is hard to imagine anyone in the programming world these days who has not heard of Ruby. The ever-increasing popularity of the Ruby on Rails web framework is helping to make Ruby the language of choice for rapid application development and testing. Ruby is an interpreted scripting language that provides quick and easy object-oriented programming and contains some neat features such as closures, blocks, and mixins. Ruby is also highly portable, running on Unix/Linux, Windows, and MacOS. For those wanting a more thorough introduction to Ruby, you can read W. Jason Gilmore's article on Ruby.
REXML: XML Made Simple for Ruby
REXML is a pure Ruby XML processor, inspired by the Electric XML library for Java, which features an easy-to-use API, small size, and speed. It supports both tree and stream document parsing. Stream parsing is about 1.5 times faster than tree parsing. However, with stream parsing, you don't get access to features such as XPath.
Getting Started with REXML
To begin working with REXML, you need to include it within your Ruby file:
require "rexml/document" include REXML # so that we don't have to prefix everything # with REXML::...
This includes the REXML library and includes the REXML namespace, so you do not need to prefix method calls with the 'REXML' prefix.
Now, create and print a simple XML document with REXML. Enter the following Ruby code into a file named 'REXMLtest.rb' and save it:
require "rexml/document" include REXML # so that we don't have to prefix everything # with REXML::... string = <<EOF <xml> <element attribute="attr">My first REXML document</element> </xml> EOF doc = Document.new string print doc
From the command line, enter the following to run 'REXMLtest.rb' and see the results:
You created a string containing a simple XML document. You then created a REXML Document object, which was initialized with the string. Finally, you printed out the XML document.
Tree Parsing and Accessing XML Elements
Now, parse an XML document and see how REXML provides access to the elements within an XML document. First, create an XML document, 'guitars.xml', as shown below:
<guitars title="My Guitars"> <make name="Fender"> <model sn="123456789" year="2006" country="japan"> <name>62 Reissue Stratocaster</name> <price>750.00</price> <color>Fiesta Red</color> </model> <model sn="112233445" year="2006" country="mexico"> <name>60s Reverse Headstock Stratocaster</name> <price>699.00</price> <color>Olympic White</color> </model> </make> <make name="Squier"> <model sn="445322344" year="2003" country="China"> <name>Standard Stratocaster</name> <price>179.99</price> <color>Cherry Sunburst</color> </model> </make> </guitars>
Read in and print 'guitars.xml' using REXML. Create a Ruby file called 'REXMLFileTest.rb':
require "rexml/document" include REXML # so that we don't have to prefix everything # with REXML::... doc = Document.new File.new("guitars.xml") print doc
You should see the following printed out when you run 'REXMLFileTest.rb':