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. ...