0

I'm only getting value1 when using .findall('string') and rest is ignored. How to get whole value? xml file input:

<resources>
 <string name="key">value1 <b>value2</b>. value3</string>
</resources>

python code:

import xml.etree.ElementTree as ET
tree = ET.parse(xml_file)
root = tree.getroot()
for string in root.findall('string'):
       name = string.get('name')
       value = string.text
0

1 Answer 1

2

You can do it with itertext()

for string in root.findall('string'):
    value = ''.join(string.itertext())
    print(value)

# output:
# value1 value2. value3
Sign up to request clarification or add additional context in comments.

1 Comment

An alternative to itertext could be value = ET.tostring(string, method='text') print(value.decode()). This solution works also, if the tag have tail text, itertext() doesn’t.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.