Skip to content Skip to sidebar Skip to footer

How To Extract Text In Nested Xml After Closing Of One Tag In Python Using Xml.etree.ElementTree

I want to extract all text in xml document, and I am having a problem for the following case: ... hello there How was your day. ..... In

Solution 1:

You are looking for the .tail attribute of an element:

>>> from xml.etree import ElementTree
>>> example = ElementTree.fromstring('''\
... <a>
... hello
... <B>
... there
... </B>
... How was your day.
... </a>
... '''
... )
>>> example
<Element 'a' at 0x10715d150>
>>> example.text
'\nhello\n'
>>> example.find('B')
<Element 'B' at 0x10715d7d0>
>>> example.find('B').text
'\nthere\n'
>>> example.find('B').tail
'\nHow was your day.\n'

Post a Comment for "How To Extract Text In Nested Xml After Closing Of One Tag In Python Using Xml.etree.ElementTree"