XML technologies / XML / Perl & XML / DOM / DOM and Perl
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
DOM is a World Wide Web Consortium (W3C) Recommendation. Designed from the start as a language-neutral interface representing XML documents in memory, it has versions in languages as varied as Java, ECMAscript, a standardized JavaScript version, Perl, and others. Perl even has several implementations, including XML::DOM and XML::LibXML.
While SAX uses handler methods as its interface, DOM consists of classes and methods representing particular XML markup types. Each instance represents part of the document tree and provides methods to add, remove, or change nodes. These objects are normally created by a factory object. Only the factory needs initialization; it configures all the other objects.
In DOM, each piece of XML—elements, text, comments, and so on—is a node represented by an instance of Node. The class Node has more specific subclasses for different markup types, such as Element, Attr (attribute), ProcessingInstruction, Comment, EntityReference, Text, CDATASection and Document. These classes are the building blocks of every DOM XML tree.
The standard also defines containers for other nodes, making XML fragments easy to move or copy. These include NodeList, an ordered list such as an element’s child nodes, and NamedNodeMap, an unordered set of nodes. These classes often appear as method argument or return types. Note that they are usually live: changes to them immediately affect the document. They are actual document parts rather than copies.
Beyond class and method names, arguments, and return types, DOM leaves many details, particularly internals, to implementations. Memory management, data structures, and algorithms are outside its scope because languages and users’ needs vary greatly. DOM resembles a key specification: a locksmith can build a matching lock. You know the key opens the door without knowing how. The interface lets you build an application on the module; efficiency and speed are separate concerns.
DOM is a very extensive standard, and implementations conform to widely varying degrees. Two versions, soon to be three, complicate matters. DOM1 dates from 1998, DOM2 is newer, and DOM3 is being developed. As with SAX, the main difference between versions 1 and 2 is namespace support. If namespaces do not matter to you, DOM1 is sufficient.
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