[XML-SIG] Simple DOM question (writing DOM)

Martin v. Loewis martin@v.loewis.de
Wed, 12 Dec 2001 07:13:52 +0100


> I'm quite new to actually using XML and I'm confused. Parsing a XML
> document and building a data structure went fine, but now I want to give
> my data structure a toxml() method that creates an Element again.

Is that a custom data structure, or a DOM tree? By "create an
Element", do you mean you want to create some data structure, or an
XML file (i.e. a sequence of bytes in a stream, or on disk)?

> How would I create an element? With the createElement method of the
> Document interface? Now, the problem is that I don't have a Document
> yet. And if I want to create one, I wouldn't even know how to create an
> empty one.

It sounds like you want to serialize your data structure into XML. The
best approach to do that is print/write:

print "<data>%s</data>" % data

> Or is there an example of writing a DOM tree somewhere and I've missed
> it?

If you do have a DOM tree already, you also should have a Document
object - minidom.parse returns one. Calling .toxml() on it will return
a byte string.

HTH,
Martin