简体   繁体   中英

How to save an xml file to disk?

我做了类似的东西这个 ,但不能找到一种方法,结果写入到一个XML文件。

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.

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')
f = open('yourfile.xml', 'w')
xml.dom.ext.PrettyPrint(doc, f)
f.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM