ElementTree, how to get the whole content of a tag

Fredrik Lundh fredrik at pythonware.com
Wed Mar 16 17:31:12 EST 2005


Damjan <gdamjan at gmail.com> wrote:

> Given the folowing XML snippet, I build an ElementTree instance with
> et=ElementTree.fromstring(..). Now et.text returns just '\n  text\n  some
> other text'.
> Is there any way I could get everything between the <div> and </div> tag?
>
> <div>
>  text
>  some other text<br/>
>  and then some more
> </div>

def gettext(elem):
    text = elem.text or ""
    for subelem in elem:
        text = text + gettext(subelem)
        if subelem.tail:
            text = text + subelem.tail
    return text

>>> gettext(et)
'\n  text\n  some other text\n  and then some more\n'

</F> 






More information about the Python-list mailing list