XML doc generation

Martin von Loewis loewis at informatik.hu-berlin.de
Tue Jan 23 16:31:08 EST 2001


Jonathan Pennington <john at coastalgeology.org> writes:

> You can also construct a DOM tree yourself, and convert it to XML;
> this is often a more flexible way of producing XML output than simply
> writing <tag1>...</tag1> to a file. 
> <snip>
> Unfortunately, I can't find an example of this.
> 
> Does anyone have some code(script?) I can read that does this so I can
> learn?

Although I agree with Paul's comments in general, I'd still point out
how to construct a tree properly.

In the DOM, there is an object called the implementation, of type
DOMImplementation. In PyXML, 4DOM offers its implementation as
xml.dom.implementation. minidom, as of Python 2.0, does not provide an
implementation; 2.1 will.

An implementation allows you to create a document, via
.createDocument. (In minidom, just creating a minidom.Document
instance will suffice).

Once you have a document, you use its createElement, createAttribute
etc. operations to create, well, elements, attributes, etc. Then you
use the appendChild method of the node to put the elements into place.

To serialize, different implementations provide different API; minidom
offers a .to_xml() method.

To see an example, look at demo/dom/generate_html1.py, or
test/test_minidom.py.

Regards,
Martin




More information about the Python-list mailing list