4.7 Building A Document

Another set of common applications requires generating new XML documents, not parsing existing documents. A document can be constructed by simply building a string containing XML tags and content, or even by sending XML output directly to a file. However, this can get messy in complicated cases, such as traversing a data structure and building an XML document based on its contents. The Builder class in the xml.dom.builder module provides an easy shortcut for generating a DOM tree from scratch. You can then call the toxml() method on the root Document instance to get the text form of the document.

The Builder class can often be used without subclassing it. A new Builder instance starts with an empty DOM tree. The startElement() and endElement() methods are used to begin and end an element. startElement() selects the created Element node as the current one, and the comment(), processingInstruction() and text() methods all nodes of the corresponding type and add them as a child of the current node. The endElement() method

For example, the following code manually constructs an HTML-like tree:

from xml.dom.builder import Builder
b = Builder()

# Create the root element
b.startElement("html")

# Create an empty 'head' element
b.startElement('head') ; b.endElement('head')

# Start the 'body' element, giving it an attribute
b.startElement('body', {'background': '#ffffff'})

# Add a text node
b.text("The body text goes here.")

# Close the body element
b.endElement("body")

At any time, the DOM tree being constructed can be retrieved as the document attribute of the Builder instance. The DOM tree resulting from the above code looks like this when dumped:

<DOM Document; root=<Element 'html'> >
 <Element 'html'>
  <Element 'head'>
  <Element 'body'>
   <Text node 'The body text goe...'>

Rendered with the toxml() method and pretty-printed, the document looks like:

<?xml version="1.0"?>
<html>
  <head/>
  <body background='#ffffff'>
    The body text goes here.
  </body>
</html>