Pablo Pablo - 1 year ago 88
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?


<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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download