Recently, I had a rather frustrating experience with REXML, the XML parser that Ruby >=1.8.4 (and probably older versions, too) ship with. My problem was the extravagently shitty documentation its maintainers provide. This ‘documentation’ is a big tutorial which is unclear on many things.

So, in an effort to help the next hapless programmer who is desperately Googling for some REXML help that isn’t retarded, I will provide you with the following.

# Load REXML
require 'rexml/document'

# Parse our XML
document = REXML::Document.new(some_xml_string)

# Iterate through the root of the document.
# NOTE - I AM NOT USING 'EACH' - SEE BELOW
document.elements[1].each_element do |element|
  # We can access the children of our element
  # with The elements[] array. Same goes for
  # attributes - user attributes[].

  puts element.elements['author']
end

The ‘each’ thing is what got me - each_element loops through and gives you something usable, while each goes through and returns REXML objects with the XML still in them. For example, each would give me ‘ . . . ‘. From there, you cannot use the elements or attributes arrays to get to the children.