简体   繁体   中英

Modify value xml tag with python

I have this xml:

<resources>
    <string name="name1">value1</string>
    <string name="name2">value2</string>
    <string name="name3">value3</string>
    <string name="name4">value4</string>
    <string name="name5">value5</string>
</resources>

and I want to change each value of each string tag, I've tried with ElementTree but i can not solve it...

I have this but it doesn't works!

tree = ET.parse(archivo_xml)
root = tree.getroot()
        
cadena = root.findall('string')
cadena.text = "something"

The root.findall() does return a list which is why that approach doesn't work.

Use root.iter() to find all the matching tags with 'string' instead, then loop over the results and change the text value of each.

for cadena in root.iter('string'):
    cadena.text = "something"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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