sms169
sms169

Reputation: 143

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. 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.

Upvotes: 1

Views: 606

Answers (2)

John Machin
John Machin

Reputation: 82934

Consider NOT using "a simple file diff". See this SO question.

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336178

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.

Upvotes: 1

Related Questions