Hi guys,

a not so complex question:

form xml.etree.ElementTree import *
>>> tostring("<home><room>bedroom</room></home>")

I would like to have the indentation in for the string. Something like:


Of course my document is a little complex. Is it possible to have it in an automatic way? And if not, could you suggest me an easy way to do it?

Thank you.

I know a method which uses the (great) lxml module instead of ElementTree. The lxml.etree.tostring() method accepts a 'pretty_print' keyword argument. See the example here http://codespeak.net/lxml/tutorial.html#serialisation .
In your case

>>> from lxml import etree
>>> root = etree.XML("<home><room>bedroom</room></home>")
>>> print(etree.tostring(root, pretty_print=True))
This question has already been answered. Start a new discussion instead.