XML technologies / XML / Perl & XML / XML basics: Reading and writing / Event-driven versus tree-based processing
(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)
Remember Perl’s motto, “There’s more than one way to do it”? It applies to XML too. Your preferred working style and available resources determine the options. One developer might prefer a simple, maintainable solution even if it wastes memory. Another might emphasize speed and resource conservation despite much greater program complexity. XML processing tasks differ greatly, so choose the right solution afresh each time.
XML processing strategies mostly fall into two categories: event-driven and tree-based. With the event-driven strategy, the parser repeatedly calls the program, providing information about document components. Like a pipeline, the XML parser feeds XML markup into one end, and processed data tumbles into your program at the other. This pipeline is called an event stream, which is why the model is also called stream-oriented. Each event and its data tells the program something interesting about the XML stream. The start of an element is significant, as is finding a processing instruction. Each event may trigger actions: translating or forwarding data, checking specific content, or storing it in a growing repository.
With the tree-based strategy, the parser keeps the data until it reaches the end. This data represents the document, returned to the program as a whole. A camera taking a photograph is a better analogy than a pipeline. Working with a data-structure representation is usually much more convenient than raw XML. Familiar Perl arrays and hashes can represent nested XML structures, as the previous example’s output showed. Trees of objects with navigation methods are often even more useful. Crucially, your program can inspect exactly the data that matters, in any order.
Why two models? Each has strengths and weaknesses. Event streams are fast and economical with memory, but usually harder to implement and require you to create storage structures for relevant data. A tree preserves data as long as needed and permits navigation forwards and backwards, removing the need to store information yourself. Trees, however, have clear weaknesses in economical CPU and memory use.
These considerations are relative. Small documents do not challenge ordinary computers, CPUs get faster and megabytes cheaper every day, and persistent structures offer overwhelming advantages here. With huge documents containing entire books, however, you will certainly notice the cost of a tree model. Event streams pay off quickly in such cases. Beyond these remarks, no rule of thumb is possible, so we leave the choice to you.
The relationship between the models is interesting: events underpin trees. Tree construction internally receives events. Most low-level parsers therefore generate event streams, from which trees are readily derived. Most parsers, including XML::Parser, work this way.
Similarly, XML event streams can convert arbitrary document types to XML through parsers mapping their data structures to XML events. This is a newer, genuinely cool development.
There is much more to say about streams and trees, so we devote two whole sections to them. In Event streams we examine event-stream theory through many useful program examples. In Tree-based processing we venture deeper into a forest of tree-based examples. Then Working with trees: XPath, XSLT and more shows unusual hybrid applications combining the best of both worlds.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Perl & XML” is subject to the same conditions as the bound edition: the work, including all its parts, is 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