XML technologies / XML / Perl & XML / Event streams / Events and handlers
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
Why call this an event stream? Why not an element stream or markup-object stream? XML is hierarchical: elements contain elements. Treating each element as one token would not work. Every element in a well-formed document is inside the root, and a root containing the whole document is not a stream. Except for empty elements, an element therefore cannot be a single token.
XML streams therefore consist of events. An event signals a change in the document state known so far. A start-tag signals an opened element and increased nesting depth. An end-tag closes the most recent open element. A processor can keep open elements on a stack: opening pushes an element, closing removes it, and stack depth equals nesting depth.
Parsers support different events with some overlap. One may distinguish start-tags and empty elements; another reports a start followed by an end in both cases. Either way, the element’s presence is reported. The following example illustrates an event stream.
Example: An XML fragment
<?xml version="1.0" encoding="ISO-8859-1"?>
<rezept>
<name>Sandwich aus Erdnußbutter und Marmelade</name>
<!-- Hier bitte ein Bild des Sandwich einfügen. -->
<zutaten>
<zutat>Erdnußbutter der Marke WasAuchImmer™</zutat>
<zutat>Brot</zutat>
<zutat>Marmelade</zutat>
</zutaten>
<anweisungen>
<vorgang>Streiche die Erdnußbutter auf eine Scheibe Brot.</vorgang>
<vorgang>Streiche die Marmelade auf eine andere Scheibe Brot.</vorgang>
<vorgang>Lege die beiden Scheiben aufeinander, so daß sich
Erdnußbutter und Marmelade berühren.</vorgang>
</anweisungen>
</rezept>Parsing this document should produce events like these:
The start of a document, not a fragment
A start-tag for the element
<rezept>A start-tag for the element
<name>Text, “Sandwich aus Erdnußbutter und Marmelade”
The end-tag of the element
<name>A comment containing “Hier bitte ein Bild des Sandwich einfügen.”
A start-tag for the element
<zutaten>A start-tag for the element
<zutat>The text “Erdnußbutter der Marke WasAuchImmer”
A reference to the entity “trade”
The end-tag of the element
<zutat>
…and so on until the final event, the end of the document.
Between tokenization and processing lies a dispatching layer, which selects processing according to token type. The code processing a particular type is a handler. There may be separate handlers for start-tags, character data, and so on, or one handler containing a complex if statement choosing subroutines by token type, making it a dispatcher itself. Alternatively, parser internals may dispatch and generate other events, as the stream mode of XML::Parser does. Normally, you register one subroutine per event type and let the parser call the right one. The strategy depends on your preferences and the parser.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top