KevinDoUrden
KevinDoUrden

Reputation: 144

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"

Upvotes: 0

Views: 1066

Answers (1)

DNy
DNy

Reputation: 799

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"

Upvotes: 1

Related Questions