Generating XML.

Alex Martelli aleax at aleax.it
Tue Mar 4 03:55:42 EST 2003


TuxedoKamen wrote:

>     Hello.  I have a program that needs to do the following:
> 1. Reads an XML file using the minidom interface
> 2. Proceeds to present the information to the user for viewing
> 3. Allows the user to update the information if desired
> 4. If user made any changes, saves updated information to XML file.
> 
>     I'm familiar with how to do one, two, and three, but four has me a
> little confused.  I am assuming it is possible to manipulate the various
> nodes of the dom tree as necessary (this would certainly make editing the
> XML easy), but I don't see any mention of exporting the tree to an
> external
> textfile.  Is this possible?  If not, what's the best way to get a DOM
> tree out of an application and into a text file?

minidom documents expose methods .toxml and .toprettyxml that you
can use for your purposes.  For example:

>>> import xml.dom.minidom
>>> tree = xml.dom.minidom.parseString('<foo>one<bar>two</bar>3</foo>')
>>> print tree.toxml()
<?xml version="1.0" ?>
<foo>one<bar>two</bar>3</foo>
>>> print tree.toprettyxml()
<?xml version="1.0" ?>
<foo>
        one
        <bar>
                two
        </bar>
        3
</foo>

>>>


Alex







More information about the Python-list mailing list