Richard Richard - 3 months ago 7x
Python Question

Get all text from an XML document?

How can I get all the text content of an XML document, as a single string - like this Ruby/hpricot example but using Python.

I'd like to replace XML tags with a single whitespace.


You asked for lxml:

reslist = list(root.iter())
result = ' '.join([element.text for element in reslist]) 


result = ''
for element in root.iter():
    result += element.text + ' '
result = result[:-1] # Remove trailing space