This is a tutorial for using REXML, a pure Ruby XML processor.
REXML was inspired by the Electric XML library for Java, which features an easy-to-use API, small size, and speed. Hopefully, REXML, designed with the same philosophy, has these same features. I've tried to keep the API as intuitive as possible, and have followed the Ruby methodology for method naming and code flow, rather than mirroring the Java API.
REXML supports both tree and stream document parsing. Stream parsing is faster (about 1.5 times as fast). However, with stream parsing, you don't get access to features such as XPath.
The API documentation also contains code snippits to help you learn how to use various methods. This tutorial serves as a starting point and quick guide to using REXML.
We'll start with parsing an XML document
Line 3 creates a new document and parses the supplied file. You can also do the following
So parsing a string is just as easy as parsing a file. For future
examples, I'm going to omit both the require
and
include
lines.
Once you have a document, you can access elements in that document in a number of ways:
Element
class itself has
each_element_with_attribute
, a common way of accessing
elements.Element.elements
is an
Elements
class instance which has the each
and []
methods for accessing elements. Both methods can
be supplied with an XPath for filtering, which makes them very
powerful.Element
is a subclass of Parent, you can
also access the element's children directly through the Array-like
methods Element[], Element.each, Element.find,
Element.delete
. This is the fastest way of accessing
children, but note that, being a true array, XPath searches are not
supported, and that all of the element children are contained in
this array, not just the Element children.Here are a few examples using these methods. First is the source document used in the examples. Save this as mydoc.xml before running any of the examples that require it:
Notice the second-to-last line of code. Element children in REXML
are indexed starting at 1, not 0. This is because XPath itself counts
elements from 1, and REXML maintains this relationship; IE,
root.elements['*[1]'] == root.elements[1]
. The last line
finds the first child element with the name of "food". As you can see
in this example, accessing attributes is also straightforward.
You can also access xpaths directly via the XPath class.
Another way of getting an array of matching nodes is through Element.elements.to_a(). Although this is a method on elements, if passed an XPath it can return an array of arbitrary objects. This is due to the fact that XPath itself can return arbitrary nodes (Attribute nodes, Text nodes, and Element nodes).
REXML attempts to make the common case simple, but this means that the uncommon case can be complicated. This is especially true with Text nodes.
Text nodes have a lot of behavior, and in the case of internal entities, what you get may be different from what you expect. When REXML reads an XML document, in parses the DTD and creates an internal table of entities. If it finds any of these entities in the document, it replaces them with their values:
When you write the document back out, REXML replaces the values with the entity reference:
But there's a problem. What happens if only some of the words are also entity reference values?
Well, REXML does the only thing it can:
This is probably not what you expect. However, when designing REXML, I had a choice between this behavior, and using immutable text nodes. The problem is that, if you can change the text in a node, REXML can never tell which tokens you want to have replaced with entities. There is a wrinkle: REXML will write what it gets in as long as you don't access the text. This is because REXML does lazy evaluation of entities. Therefore,
There is a programmatic solution: :raw
. If you set the
:raw
flag on any Text or Element node, the entities
within that node will not be processed. This means that you'll have to
deal with entities yourself:
Again, there are a couple of mechanisms for creating XML documents in REXML. Adding elements by hand is faster than the convenience method, but which you use will probably be a matter of aesthetics.
If you want to add text to an element, you can do it by either
creating Text objects and adding them to the element, or by using the
convenience method text=
But note that each of these text objects are still stored as
separate objects; el1.text
will return "Hello world!";
el1[2]
will return a Text object with the contents
"Goodbye".
Please be aware that all text nodes in REXML are UTF-8 encoded, and all of your code must reflect this. You may input and output other encodings (UTF-8, UTF-16, ISO-8859-1, and UNILE are all supported, input and output), but within your program, you must pass REXML UTF-8 strings.
I can't emphasize this enough, because people do have problems with
this. REXML can't possibly alway guess correctly how your text is
encoded, so it always assumes the text is UTF-8. It also does not warn
you when you try to add text which isn't properly encoded, for the
same reason. You must make sure that you are adding UTF-8 text.
If you're adding standard 7-bit ASCII, which is most common, you
don't have to worry. If you're using ISO-8859-1 text (characters
above 0x80), you must convert it to UTF-8 before adding it to an
element. You can do this with the shard:
text.unpack("C*").pack("U*")
. If you ignore this warning
and add 8-bit ASCII characters to your documents, your code may
work... or it may not. In either case, REXML is not at fault.
You have been warned.
One last thing: alternate encoding output support only works from Document.write() and Document.to_s(). If you want to write out other nodes with a particular encoding, you must wrap your output object with Output:
You can pass Output any of the supported encodings.
If you want to insert an element between two elements, you can use
either the standard Ruby array notation, or
Parent.insert_before
and
Parent.insert_after
.
The raw
flag in the Text
constructor can
be used to tell REXML to leave strings which have entities defined for
them alone.
Note that, in all cases, the value()
method returns
the text with entities expanded, so the raw
flag only
affects the to_s()
method. If the raw
is set
for a text node, then to_s()
will not entities will not
normalize (turn into entities) entity values. You can not create raw
text nodes that contain illegal XML, so the following will generate a
parse error:
You can also tell REXML to set the Text children of given elements to raw automatically, on parsing or creating:
In this example, all tags named "tag1", "tag2", or "tag3" will have any Text children set to raw text. If you want to have all of the text processed as raw text, pass in the :all tag:
There aren't many things that are more simple than writing a REXML
tree. Simply pass an object that supports <<( String
)
to the write
method of any object. In Ruby, both
IO instances (File) and String instances support <<.
If you want REXML to pretty-print output, pass write()
an indent value greater than -1:
REXML will not, by default, write out the XML declaration unless
you specifically ask for them. If a document is read that contains an
XML declaration, that declaration
There are four main methods of iterating over children.
Element.each
, which iterates over all the children;
Element.elements.each
, which iterates over just the child
Elements; Element.next_element
and
Element.previous_element
, which can be used to fetch the
next Element siblings; and Element.next_sibling
and
Eleemnt.previous_sibling
, which fetches the next and
previous siblings, regardless of type.
REXML stream parsing requires you to supply a Listener class. When REXML encounters events in a document (tag start, text, etc.) it notifies your listener class of the event. You can supply any subset of the methods, but make sure you implement method_missing if you don't implement them all. A StreamListener module has been supplied as a template for you to use.
Stream parsing in REXML is much like SAX, where events are
generated when the parser encounters them in the process of parsing
the document. When a tag is encountered, the stream listener's
tag_start()
method is called. When the tag end is
encountered, tag_end()
is called. When text is
encountered, text()
is called, and so on, until the end
of the stream is reached. One other note: the method
entity()
is called when an &entity;
is
encountered in text, and only then.
Please look at the StreamListener
API for more information.
By default, REXML respects whitespace in your document. In many applications, you want the parser to compress whitespace in your document. In these cases, you have to tell the parser which elements you want to respect whitespace in by passing a context to the parser:
Whitespace for tags "tag1", "tag2", and "tag3" will be compressed; all other tags will have their whitespace respected. Like :raw, you can set :compress_whitespace to :all, and have all elements have their whitespace compressed.
You may also use the tag :respect_whitespace
, which
flip-flops the behavior. If you use :respect_whitespace
for one or more tags, only those elements will have their whitespace
respected; all other tags will have their whitespace compressed.
REXML does some automatic processing of entities for your convenience. The processed entities are &, <, >, ", and '. If REXML finds any of these characters in Text or Attribute values, it automatically turns them into entity references when it writes them out. Additionally, when REXML finds any of these entity references in a document source, it converts them to their character equivalents. All other entity references are left unprocessed. If REXML finds an &, <, or > in the document source, it will generate a parsing error.
Namespaces are fully supported in REXML and within the XPath parser. There are a few caveats when using XPath, however:
each
, first
, and
match
and pass them the mapping.The pull parser API is not yet stable. When it settles down, I'll fill in this section. For now, you'll have to bite the bullet and read the PullParser API docs. Ignore the PullListener class; it is a private helper class.
The original REXML stream parsing API is very minimal. This also means that it is fairly fast. For a more complex, more "standard" API, REXML also includes a streaming parser with a SAX2+ API. This API differs from SAX2 in a couple of ways, such as having more filters and multiple notification mechanisms, but the core API is SAX2.
The two classes in the SAX2 API are SAX2Parser
and SAX2Listener
.
You can use the parser in one of five ways, depending on your needs.
Three of the ways are useful if you are filtering for a small number
of events in the document, such as just printing out the names of all
of the elements in a document, or getting all of the text in a
document. The other two ways are for more complex processing, where
you want to be notified of multiple events. The first three involve
Procs, and the last two involve listeners. The listener mechanisms are
very similar to the original REXML streaming API, with the addition of
filtering options, and are faster than the proc mechanisms.
An example is worth a thousand words, so we'll just take a look at a small example of each of the mechanisms. The first example involves printing out only the text content of a document.
In this example, we tell the parser to call our block for every
characters
event. "characters" is what SAX2 calls Text
nodes. The event is identified by the symbol :characters
.
There are a number of these events, including
:element_start
, :end_prefix_mapping
, and so
on; the events are named after the methods in the
SAX2Listener
API, so refer to that document for a
complete list.
You can additionally filter for particular elements by passing an
array of tag names to the listen
method. In further
examples, we will not include the require
or parser
construction lines, as they are the same for all of these
examples.
In this example, only the text content of changelog and todo elements will be printed. The array of tag names can also contain regular expressions which the element names will be matched against.
Finally, as a shortcut, if you do not pass a symbol to the listen
method, it will default to :element_start
This example prints the "version" attribute of all "item" elements
in the document. Notice that the number of arguments passed to the
block is larger than for :text
; again, check the
SAX2Listener API for a list of what arguments are passed the blocks
for a given event.
The last two mechanisms for parsing use the SAX2Listener API. Like
StreamListener, SAX2Listener is a module
, so you can
include
it in your class to give you an adapter. To use
the listener model, create a class that implements some of the
SAX2Listener methods, or all of them if you don't include the
SAX2Listener model. Add them to a parser as you would blocks, and when
the parser is run, the methods will be called when events occur.
Listeners do not use event symbols, but they can filter on element
names.
In the previous example, listener1
will be notified of
all events that occur, and listener2
will only be
notified of events that occur in changelog
,
todo
, and credits
elements. We also see that
multiple listeners can be added to the same parser; multiple blocks
can also be added, and listeners and blocks can be mixed together.
There is, as yet, no mechanism for recursion. Two upcoming features of the SAX2 API will be the ability to filter based on an XPath, and the ability to specify filtering on an elemnt and all of its descendants.
WARNING: The SAX2 API for dealing with doctype (DTD) events almost certainly will change.
Michael Neumann contributed some convenience functions for nodes,
and they are general enough that I've included. Michael's use-case
examples follow:
This isn't everything there is to REXML, but it should be enough to
get started. Check the API
documentationtest/
directory,
and these are great sources of working examples.
Among the people who've contributed to this document are: