Skip to Main Content
Python & XML
book

Python & XML

by Christopher A. Jones, Fred L. Drake
December 2001
Intermediate to advanced content levelIntermediate to advanced
380 pages
11h 54m
English
O'Reilly Media, Inc.
Content preview from Python & XML

Retrieving Information

Retrieving information from a document is easy using the DOM. Most of the work lies in traversing the document tree and selecting the nodes that are actually interesting for the application. Once that is done, it is usually trivial to call a method of the node (or nodes), or to retrieve the value of an attribute of the node. In order to extract information using the DOM, however, we first need to get a DOM document object.

Getting a Document Object

Perhaps the most glaring hole in the DOM specifications is that there is no facility in the API for retrieving a document object from an existing XML document. In a browser, the document is completely loaded before the DOM client code in the embedded or linked scripts can get to the document, so the document object is placed in a well-known location in the script’s execution environment. For applications that do not live in a web browser, this approach simply does not work, so we need another solution.

Our solution depends on the particular DOM implementation we use. We can always create a document object from a file, a string, or a URL.

Loading a document using 4DOM

Creating a DOM instance to work with is easy in Python. Using 4DOM, we need call only one function to load a document from an open file:

from xml.dom.ext.reader.Sax2 import FromXmlStream
doc = FromXmlStream(sys.stdin)

Loading a document using minidom

There are two convenient functions in the xml.dom.minidom module that can be used to load a document. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

XML Processing with Python

XML Processing with Python

Sean McGrath
Python One-Liners

Python One-Liners

Christian Mayer

Publisher Resources

ISBN: 0596001282Supplemental ContentErrata Page