开发者

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

开发者 https://www.devze.com 2022-12-28 16:14 出处:网络
Suppose I want to edit a node in xmland edit one of its attributes. I want to be able to do a simple file diff to just see one row changed. Dumping the xml using prettyprint change开发者_StackOverflow

Suppose I want to edit a node in xml and edit one of its attributes. I want to be able to do a simple file diff to just see one row changed. Dumping the xml using prettyprint change开发者_StackOverflows the whole xml structure.


Well then don't use prettyprint.

In xml.dom.minidom for example, if you do

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消