I\'ve looked through several posts but I haven\'t quite found any answers that have solved my problem.
Sample XML =
Using xml.etree.ElemetTree (which is similar to lxml which @DiegoNavrro used in his answer, except that etree in part of the standard library and doesn't have XPATH etc.) you can give the following a go:
import xml.etree.ElementTree as etree
xml_string = """
TEXT1 TEXT2 TEXT3
"""
xml_etree = etree.fromstring(xml_string)
text = [element.tail for element in xml_etree]
# `text` will be ['TEXT1', 'TEXT2 ', 'TEXT3', '\n']
Note, this assumes that the XML
... is correct. Because the text follows a closing tag, it becomes the tag's tail text. It is not the elements nodeValue, which is why in your code in the question you are getting None
s.
If you wanted to parse some XML like
you would have to replace the line [element.tail for element in xml_etree]
with [element.text for element in xml_etree]
.