Alex Rivera | Logout

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

Asked 2008-12-19T10:33:52.183
12

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>
Edit
Report

1 Answer

9

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
answered 2008-12-19T17:27:09.257

Your Answer