tags:

views:

504

answers:

3

I did something similar to this, but couldn't find a way to write the result to an xml file.

+7  A: 

The code on the web page you linked to uses doc.toprettyxml to create a string from the XML DOM, so you can just write that string to a file:

f = open("output.xml", "w")
try:
    f.write(doc.toprettyxml(indent="  "))
finally:
    f.close()

In Python 2.6 (or 2.7 I suppose, whenever it comes out), you can use the "with" statement:

with open("output.xml", "w") as f:
    f.write(doc.toprettyxml(indent="  "))

This also works in Python 2.5 if you put

from __future__ import with_statement

at the beginning of the file.

David Zaslavsky
+1 for the edit to add `with`.
Devin Jeanpierre
+1  A: 
f = open('yourfile.xml', 'w')
xml.dom.ext.PrettyPrint(doc, f)
f.close()
jcoon
Thanks. This open method creates a new file?
Joan Venge
It's a function, and it will create a new file if the file doesn't already exist and 'w', 'wb', etc. are passed to it. Read the docs on it.
Devin Jeanpierre
Also do I have to import xml.dom first?
Joan Venge
Also is thi in 2.6? I get this error: ImportError: No module named ext
Joan Venge
+1  A: 

coonj is kind of right, but xml.dom.ext.PrettyPrint is part of the increasingly neglected PyXML extension package. If you want to stay within the supplied-as-standard minidom, you'd say:

f= open('yourfile.xml', 'wb')
doc.writexml(f, encoding= 'utf-8')
f.close()

(Or using the ‘with’ statement as mentioned by David to make it slightly shorter. Use mode 'wb' to avoid unwanted CRLF newlines on Windows interfering with encodings like UTF-16. Because XML has its own mechanisms for handling newline interpretation, it should be treated as a binary file rather than text.)

If you don't include the ‘encoding’ argument (to either writexml or toprettyxml), it'll try to write a Unicode string direct to the file, so if there are any non-ASCII characters in it, you'll get a UnicodeEncodeError. Don't try to .encode() the results of toprettyxml yourself; for non-UTF-8 encodings this can generate non-well-formed XML.

There's no ‘writeprettyxml()’ function, but it's trivially simple to do it yourself:

with open('output.xml', 'wb') as f:
    doc.writexml(f, encoding= 'utf-8', indent= '    ', newl= '\n')
bobince