December 21, 2014
Hot Topics:

REXML: Proccessing XML in Ruby

  • April 18, 2007
  • By Dominic Da Silva
  • Send Email »
  • More Articles »

First off, print all the colors of the guitars in this document. You do this by accessing each 'guitars/make/model/color' element of the document and printing the text contained within this element:

include REXML    # so that we don't have to prefix everything
                 # with REXML::...

doc = Document.new File.new("guitars.xml")

doc.elements.each("guitars/make/model/color")
                 { |element| puts element.text }

When you run the Ruby script again, you see the guitar colors printed out.



Click here for a larger image.

Total up the cost of all these guitars. For each price element, add it to a total, and then print the total:

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

# doc.elements.each("guitars/make/model/color")
#                  { |element| puts element.text }

total = 0

doc.elements.each("guitars/make/model/price") { |element|

   total += element.text.to_i

}


puts "Total is $" + total.to_s

When you run this script, you see the following output:



Click here for a larger image.

XPath Expressions

REXML also supports XPath. XPath provides a way to access parts of XML documents using a syntax similar to directories in filesystems of an operating system. Print the first 'model' in your list of guitars. You need to search for the first 'model' element in the XML document. To do this using REXML's XPath API, do the following:

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

firstmodel = XPath.first( doc, "//model" )

print firstmodel

XPath.first is a method that returns the first element in a collection. You use it to return the first element in the 'doc' document. You specify an XPath expression "//model", which tells XPath.first to search for all 'model' elements, starting at the root element, specified by the '//' symbol. When you run this script, you should see the following output:



Click here for a larger image.

Now, print out all the model years of the guitars. Your model years are stored as attributes named 'year' in your 'model' elements. You use the XPath.each method, passing in the XPath expression "//model/attribute::year".

require "rexml/document"
include REXML    # so that we don't have to prefix everything with
                 # REXML::...

doc = Document.new File.new("guitars.xml")

XPath.each( doc, "//model/attribute::year")
          { |element| puts element }

When you run this script, you should see the following output:



Click here for a larger image.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel