Next / Previous / Contents / TCC Help System / NM Tech homepage

13.2. The Document class

To begin creating an XML document, use this constructor to instantiate a Document object to hold the content of the document:

Document ( rootGI, doctype=None, nsMap=None )

Arguments are:


The qualified name of the root element.


A DocumentType object specifying the document type. To generate a document with no <!DOCTYPE ...> specifier, pass None as this argument.


If your document will use namespace prefixes, this argument must contain a Python dictionary that translates namespace prefixes to namespace URIs.

If you are using the default namespace, and you want to include an xmlns attribute that links the default namespace to a specific namespace URI, include in your dictionary an entry whose key is None and whose value is the namespace URI.

Here is an example of such a dictionary. Namespace prefix "xsl:" will refer to the XSLT namespace; prefix "exsl:" will refer to the EXSLT (XSLT extensions) namespace; and the default namespace will be XHTML 1.0 Strict.

{ "xsl:":  "",
   "exsl:": "",
   None:    ""

This constructor returns a new Document object with one Element child whose name is given by rootGI.

13.2.1.  Document attributes

The .root attribute of the return object is the root Element.

13.2.2.  Document methods

Methods on the Document object include:


The doctype argument that was supplied to the constructor.

.serialize ( outFile=None )

Writes the document to a given file. If supplied, outFile is a writeable file object. The default output file is sys.stdout, the standard output stream.

This method is guaranteed not to add any superfluous whitespace or line breaks to your document. Use it when the document is not intended to be human-readable, because the output may have horrendously long lines.

.write ( outFile=None )

This method is similar to .serialize(), but it may add line breaks and whitespace for indentation. Use this method to make the output somewhat more human-readable, or when extra whitespace will not change the presentation of the document.