Richard Richard - 1 year ago 77
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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download