使用minidom无法保存xml文件[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我尝试在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)是真的吗?

我如何解决我的问题?

python xml minidom
1个回答
2
投票

在我看来,lxmlminidom更好地处理XML。如果你有它,这是如何使用它:

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来调试您的代码。只需在代码中编写import pdb; pdb.set_trace(),然后在shell中运行函数,就应该停在此行。它可以让您更好地了解不起作用的内容。

© www.soinside.com 2019 - 2024. All rights reserved.