developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Data Center Solutions
Memory
Build a Server Rack
Boat Donations
Promotional Pens
Promote Your Website
Compare Prices
Corporate Awards
Calling Cards
Computer Deals
Computer Hardware
Remote Online Backup
Corporate Gifts
Holiday Gift Ideas

 


Related Article -
Ruby and Ruby on Rails Index Page
RJS Templates: Adding Some Ajax Goodness to Rails
Running Your First Rails Application on JRuby
Change your View: Enhancing the User Interface of a Ruby on Rails Application
Skip the CRUD: Building a Simple Database Backed Web Application Using Ruby on Rails
Ruby on Rails: All Aboard the Fast Train to Web Application Development
JRuby: Java and Ruby Together at Last
Ruby for the REST of Us: Using Ruby and REST to Integrate with Amazon S3
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

REXML: Proccessing XML in Ruby
By Dominic Da Silva

Go to page: 1  2  3  Next  

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:



Click here for a larger image.

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':



Click here for a larger image.

Go to page: 1  2  3  Next  

Previous article: RJS Templates: Adding Some Ajax Goodness to Rails


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Languages & Tools Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.
Data Sheet: IBM Information Server Blade
Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Whitepaper: Embeddable Content Platform for OEM's



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES