Now we move on to the design of the xml4create.py module.
Design goals include:
The caller should not have to worry about details of
implementation such as which DOM variant we're using.
Regular Python object constructors should be used to create the pieces of an XML document.
The new module manages access to the objects that
contain the factory methods. In order to create a DOM
Element, you have to have a
DOM Document element because
that's where the .createElement() method lives. But to
create a DOM Document element,
you have to have a DOMImplementation object, because that's
where the .createDocument()
method lives.
To solve the factory method access problem, the module uses these links between objects so that any node in the tree can get to the factory functions it needs:
The module's Document method
contains its DOM Document node
in its .node attribute.
Element objects also have a
.node attribute that contains
the DOM Element object.
That DOM Element object has an
.ownerDocument element that
points to its containing DOM Document object.
Now, on to the actual source code of the xml4create.py module.
First is a prologue that gives the module's documentation
string:
"""xml4create.py: For creating XML files from scratch.
For full documentation, see:
http://www.nmt.edu/tcc/help/pubs/pyxml4/
"""
Next, the imports. We need the sys module
so we can access the standard output stream, sys.stdout. Then we'll need the domlette to handle the XML functions.
#================================================================ # Imports #---------------------------------------------------------------- import sys import Ft.Xml.Domlette as domlette