how to save xml (create with fuctions in xml.dom.minidom

Alex Martelli aleaxit at yahoo.com
Wed Aug 1 13:13:19 CEST 2001


"sdf" <wqh-2 at 263.net> wrote in message
news:mailman.996659341.21784.python-list at python.org...
> belowing  is example from document,but how to save newdoc to a file,
> I use writexml,but failed.
>
> from xml.dom.minidom import Document
> newdoc = Document()
> newel = newdoc.createElement("some_tag")
> newdoc.appendChild(newel)

What do you mean by "failed"?  This sequence of operations
succeeds for me when followed by a call to newdoc.writexml(f)
where f is a file object open for writing.  You're probably
making some minor mistake elsewhere, e.g. right after this
incomplete code snippet that you quote.

Please suppy a complete copy-and-paste of a simple interactive
interpreter session that you think is failing.  Here is one
that is working just dandy:

D:\bookte>python
Python 2.1.1c1 (#19, Jul 13 2001, 00:25:06) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
Alternative ReadLine 1.4 -- Copyright 2001, Chris Gonnerman
>>> from xml.dom.minidom import Document
>>> newdoc=Document()
>>> newel=newdoc.createElement("some_tag")
>>> newdoc.appendChild(newel)
<DOM Element: some_tag at 8523836>
>>> thefile=open("bah.txt","w")
>>> newdoc.writexml(thefile)
>>> thefile.close()
>>>


D:\bookte>type bah.txt
<?xml version="1.0" ?>
<some_tag/>
D:\bookte>


Alex






More information about the Python-list mailing list