Joan Venge
Joan Venge

Reputation: 331062

How to save an xml file to disk?

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

Upvotes: 2

Views: 14778

Answers (3)

bobince
bobince

Reputation: 536399

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')

Upvotes: 9

Jason Coon
Jason Coon

Reputation: 18431

f = open('yourfile.xml', 'w')
xml.dom.ext.PrettyPrint(doc, f)
f.close()

Upvotes: 1

David Z
David Z

Reputation: 131600

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.

Upvotes: 11

Related Questions