XML technologies / XML / Perl & XML / Event streams / Stream-oriented applications

(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)

Event-oriented or stream-oriented processing is well suited to a range of common tasks, including the following:

Filter

A Filter essentially produces a copy of the input XML document with a few minor changes. For example, to turn every <A> element into a <B> element, we use a filter. Writing the corresponding handler is very simple. It outputs everything it receives, changing the tag name only when necessary in start and end tags.

Selectors

If you are interested in only a particular piece of information from the document, you can use a selector. This program examines incoming events but usually does nothing until the event it is waiting for arrives. That might be a particular tag name, an attribute, or something else. We call this signal the key of the selector. Once the key is recognized, the selector changes its behavior. It now passes the data through unchanged, like a filter. The selector can also make minor changes; the distinction from a filter is fluid. Often, further signals cause it to return to its original behavior, and it may switch back and forth between states.

Accumulators (summarizers)

This type of program reads a document to produce statistics. An accounting program might count transactions and pass them to a spreadsheet. Another program might collect section titles into a table of contents. An index generator might maintain a list of keywords or links to them. The handler normally keeps certain document data in memory and outputs it, slightly modified, once the parser finishes.

Converters

This particularly common type of program converts an XML document to another output format, which may itself be another XML application. For example, converters exist for transforming DocBook XML to HTML or XHTML. Converters usually reach the limits of event streams very quickly: the resulting program simply becomes too complicated. Conversely, when an event-based approach succeeds, the reward is particularly high: the performance gain is often enormous.

XML stream processing is therefore excellent for many common applications, but it has limits. Ultimately, the parser always controls the program and follows its own agenda. Your program must accept the data in exactly the order chosen by the parser. You cannot ask, “Wait, can we go back eight steps?” or “Could we quickly see what comes last in this element?” You can give your program a memory by building your own data structures. Well-designed structures let you access earlier events. Eventually, however, you may discover that you are effectively building your own document tree and have already moved into tree-oriented programming. In Tree-oriented processing we will therefore examine trees in greater detail.

With these foundations of XML stream processing, we can now explore more specific examples and write real applications.

  

  

 

 

 

back to top