pythonxmlelementtree

Parse XML to String with <b> tags


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

Solution

  • You can do it with itertext()

    for string in root.findall('string'):
        value = ''.join(string.itertext())
        print(value)
    
    # output:
    # value1 value2. value3