[英]How to modify different values with same tags in XML using Python?
I try to do this thing here : 我尝试在这里做这件事:
<a>
<b>
<c>
<d>
<name>VERSION</name>
<description />
<defaultValue>v1.0.0</defaultValue>
<trim>false</trim>
</d>
<d>
<name>LINK</name>
<description />
<defaultValue>current</defaultValue>
<trim>false</trim>
</d>
</c>
<b>
</a>
<a>
<b>
<c>
<d>
<name>VERSION</name>
<description />
<defaultValue>v2.0.0</defaultValue>
<trim>false</trim>
</d>
<d>
<name>LINK</name>
<description />
<defaultValue>I changed the link</defaultValue>
<trim>false</trim>
</d>
</c>
<b>
</a>
I have an XML file and I want to change the default values. 我有一个XML文件,我想更改默认值。 I tried with this code but didn't work and idk how to handle it :
我尝试使用此代码,但没有工作,并且idk如何处理它:
from xml.etree import ElementTree as ET
tab = ["V2.0.0, "I changed the link"]
i = 0
tree = ET.parse("myfile.xml")
for child in tree.findall(".//d"):
tree.find(".//defaultValue").text = tab[i]
i=+1
Thank you for your help ! 谢谢您的帮助 !
Try to change 尝试改变
tree.find(".//defaultValue").text = tab[i]
with 同
child.find(".//defaultValue").text = tab[i]
Add this line of code as the last line of your script in order to see the modified XML 将这行代码添加为脚本的最后一行,以查看修改后的XML
ET.dump(tree)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.