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&trade;</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:

  1. The start of a document, not a fragment

  2. A start-tag for the element <rezept>

  3. A start-tag for the element <name>

  4. Text, “Sandwich aus Erdnußbutter und Marmelade”

  5. The end-tag of the element <name>

  6. A comment containing “Hier bitte ein Bild des Sandwich einfügen.”

  7. A start-tag for the element <zutaten>

  8. A start-tag for the element <zutat>

  9. The text “Erdnußbutter der Marke WasAuchImmer”

  10. A reference to the entity “trade”

  11. 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.

  

  

 

 

 

back to top