there are many file in a folder named “ini” an i have to search a file with attribute name
and edit its value, i’ve tried this code:-
import os, glob
from lxml import etree
for filename in glob.glob("./ini/*.xml"):
xmlData = etree.parse(filename)
for msg in xmlData.findall("//Value"):
name = msg.attrib["name"]
init = msg.attrib["initValue"]
if name == "attribute name" :
msg.set("initValue", "0")
print msg.get('name'), msg.get('initValue')
print msg.attrib
print name, init
print filename
the value is updated only in “msg” not in original file
So you have reached a point where you have read, parsed and modified the xml file. The last step is missing which is to save the file since the modified version only exists in the memory of your running process.
This will write the modified xml to outfile.xml in a pretty printed (indented) format. You will be able to overwrite the original file by replacing “outfile.xml” with the variable containing your original filename.