[XML-SIG] Generating XML documents
Thu, 12 Oct 2000 10:25:37 +0200 (CEST)
On Wed, 11 Oct 2000, Patrick Blanchette wrote:
> I'm a newbie in xml. I want to generate new xml documents using
> python code. In the HOWTO doc, there is a "xml.dom.builder" class but
> this class did not seem to be part of the PyXML 0.6.1.
> Where can I found a python base class for generating xml documents?
Actually, generating XML document is pretty easy: you just have to build a
string which begins with '<?xml version="1.0"?>' and add your tags in the
string, and flush it to disk. This works well if you know in advance what
you want oin your XML document.
OTOH, if you want to build your document incrementally, you can use DOM.
you've got to import the DOM implementation first:
>>> from xml.dom import implementation
and build a document from the implementation:
>>> docType = implementation.createDocumentType('','','')
>>> doc = implementation.createDocument('',None,docType)
Then you can set your root element:
>>> root = doc.createElementNS('','docRoot')
And then you can use the createXXX methods of your document to create new
nodes and use appendChild to add them to other nodes. If you want to set
attributes, you can use the setAttributeNS method in Element. Beware if
you're using python1.5.2, you cannot use no ASCII characters as arguments
to createTextNode() or setAttributeNS() (at least not if you intend to
save your file to disk), since these expect UTF-8 strings, and not
When you're done creating your document, use Print or PrettyPrint to save
it to disk:
>>> from xml.dom.ext import PrettyPrint
If you want to reload your file, use the readers:
>>> from xml.dom.ext.reader import Sax2
>>> doc = Sax2.FromXmlFile('/tmp/test.xml')
If you saved it with PrettyPrint, the new dom tree will have text nodes
full of whitespace. You can use the StripXml extension to clean the tree:
>>> from xml.dom.ext import StripXml
I'd suggest that you check the W3C DOM spec (at least core DOM), since the
DOM implementation in PyXml0.6 in a good implementation of the spec. I'll
be glad to help you if you have further questions with DOM.
http://www.logilab.com - "Mais oł est donc Ornicar ?" -
LOGILAB, Paris (France).