Ruby Programming/XML Processing/REXML

REXML is a XML processing API. As of Ruby 1.8, it is included in the Standard API.

REXML can read and write XML documents. Validation against a DTD or a schema is not yet fully implemented.

Strong XML parsing gems exist as well, such as nokogiri and the hpricot gems. They work faster than REXML typically.

Definitions
Using the DOM API, REXML can parse documents and build a tree containing the elements, attributes, and texts.

For example, this might be used to save a wikibook:

In this case, the  is an element. It has an attribute  with the value   and a text with the value "Ruby is a programming language [...]".

The  is also an element. It has an attribute, too, but no text. Instead, it has an element, the  element.

In short, elements can have attributes, text and child elements.

Representation in REXML
When parsing a XML document, an instance of the  class is created. (The  message of   just has to be fed with a   itself, or a , or an  .) This represents the whole document, including the   tag. itself is a subclass of  , an important class.

When using DOM, instances of the  class are representing the elements of the XML document. They might have attributes, accessible using the  message, text, and child elements.

The  is an   itself, but usually, you might be more interested in the root element of the XML document. As defined in the XML specification, any document has only one root element; it can be easily obtained calling.

Once you have obtained the root, you can go down the tree using the   message defined in  , which returns a collection of all child elements, or access attributes or texts, whatever you need.

The tree can be modified, too. In addition, the  methods have been overridden to return the XML code of elements, attributes and text. returns the XML code of the whole element, including attributes, text, and child elements' XML code. You can call that on the, too.