XML technologies / XML / Python & XML / The Document Object Model / Python options for DOM
(Excerpt from "Python & XML" by Christopher A. Jones & Fred L. Drake, Jr.)
Python offers several other ways to work with DOM. Choose the one that best meets your needs. minidom is smaller and faster than a full DOM, yet meets the needs of most users. pulldom lets you use only the part of DOM needed for a particular application, making DOM easier to use with very large documents or strict memory limits. 4DOM is a complete implementation of DOM Level 2. Although these are the leading Python DOM implementations and the only ones described here, other implementations are available that may suit your needs even better.
Minidom
minidom is part of the xml.dom package in both the Python standard library and PyXML, and is a lightweight DOM implementation. It aims to offer a simple implementation with lower memory requirements than a full DOM can provide. The methods for working with DOM are also simple. minidom also provides functions for working with XML fragments in strings and methods for extracting them.
Overall, minidom may be best suited to simple configuration files, which need not be small, whether for loading into applications, submitting web forms, authorizing users, or anywhere else that needs a little XML. With minidom you can reduce memory and time overhead, two crucial considerations in web application development.
Pulldom
pulldom, which can also be imported from the xml.dom package, could be a lifesaver when you need to take part of a huge XML document and create a DOM instance for that subset for further processing. pulldom allows DOM fragments to be constructed from SAX events. The module uses minidom for the actual nodes it returns.
pulldom aims to be a middle ground between DOM and SAX. It seeks to eliminate SAX state management, including the location markers mentioned earlier, while retaining stream-based processing for speed and efficiency. pulldom also aims to simplify the self-similar, complex nature of a full DOM tree, with its many nodes and lists, and reduce its appetite for memory.
4DOM
Both minidom and pulldom have their own particular uses, but for the rest of this book we will work with 4DOM. This DOM implementation supports most Level 2 features that are useful outside a browser.
After your earlier experience with SAX in this section, interacting with DOM may seem incredibly simple by comparison. But dealing with the apparently endless intricacy of nested node classes could quickly send you back to SAX for your string comparisons. Whatever your experience, the following sections introduce working with DOM in Python and provide a reference to its interfaces.
Regardless of the implementation, DOM supports two basic types of operations. The most common extract information from a document, and we explain these first. We then describe how to modify and create documents with DOM.
Copyright © 2002 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Python & 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