无法使用 minidom 保存 xml 文件

Can not save xml file using minidom

我尝试在 python 中使用 minidom 修改并保存一个 xml 文件。

除 1 个特定文件外,一切都很好,我只能读取但不能写回。

我用来保存 xml 文件的代码:

domXMLFile = minidom.parse(dom_document_filename)

#some modification

F= open(dom_document_filename,"w")
domXMLFile .writexml(F)
F.close()

我的问题是:
minidom 真的不能处理太大的文件(714KB)吗?

我该如何解决我的问题?

在我看来,lxml 在处理 XML 方面比 minidom 好得多。如果你有它,这里是如何使用它:

from lxml import etree 
root = etree.parse('path/file.xml')

# some changes to root

with open('path/file.xml', 'w') as f:
     f.write(etree.tostring(root, pretty_print=True))

如果没有,您可以使用 pdb 来调试您的代码。只需在你的代码中你想要一个 break pont 的地方写 import pdb; pdb.set_trace() 并且当 运行 你的函数在 shell 中时,它应该停止在这一行。它可以让您更好地了解什么不起作用。