Pablo Pablo - 4 months ago 9
Python Question

How do I get the full XML or HTML content of an element using ElementTree?

That is, all text and subtags, without the tag of an element itself?

Having

<p>blah <b>bleh</b> blih</p>


I want

blah <b>bleh</b> blih


element.text returns "blah " and etree.tostring(element) returns:

<p>blah <b>bleh</b> blih</p>

Answer

This is the solution I ended up using:

def element_to_string(element):
    s = element.text or ""
    for sub_element in element:
        s += etree.tostring(sub_element)
    s += element.tail
    return s
Comments