简体   繁体   English

在Python中,有没有一种简单的方法可以就地编辑xml?

[英]In Python, is there an easy way to edit xml in place?

Suppose I want to edit a node in xml and edit one of its attributes. 假设我要编辑xml中的节点并编辑其属性之一。 I want to be able to do a simple file diff to just see one row changed. 我希望能够做一个简单的文件比较,只看到一行已更改。 Dumping the xml using prettyprint changes the whole xml structure. 使用prettyprint转储xml会更改整个xml结构。

Well then don't use prettyprint. 好吧,不要使用prettyprint。

In xml.dom.minidom for example, if you do 例如,在xml.dom.minidom

doc = xml.dom.minidom.parse(inputfile)
do_something_with(doc)
outputfile.write(doc.toxml("utf-8").decode("utf-8"))

all the structure/whitespace etc. will remain unchanged. 所有结构/空白等将保持不变。

Consider NOT using "a simple file diff". 考虑不要使用“简单文件差异”。 See this SO question . 看到这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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