I construct XML using The ElementTree XML API and I would like to be able to pretty-print
- individual nodes (for inspection) as well as
- the whole document (to a file, for future examination).
I can use use ET.write()
to write my XML to file and then pretty-print it using many suggestions in Pretty printing XML in Python. However, this requires me to serialize and then deserialize the XML (to disk or to StringIO) just to serialize it again prettily - which is clearly suboptimal.
So, is there a way to pretty-print an xml.etree.ElementTree
?