Home > OS >  Modify value xml tag with python
Modify value xml tag with python

Time:11-02

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"

CodePudding user response:

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"
  • Related