classification
Title: Add xml pretty print option to ElementTree
Type: enhancement Stage: resolved
Components: Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: xml.etree.ElementTree: add feature to prettify XML output
View: 14465
Assigned To: Nosy List: eli.bendersky, rhettinger, scoder
Priority: normal Keywords:

Created on 2015-04-02 07:01 by rhettinger, last changed 2018-08-17 15:22 by mcepl. This issue is now closed.

Messages (2)
msg239879 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-04-02 07:01
The xml.dom.minidom package has as a xml.toprettyxml() function that has the problem of altering the whitespace of the text of elements.   The ElementTree module needs a prettify option that works better, perhaps something based of Effbot's code at:

http://effbot.org/zone/element-lib.htm#prettyprint

The lxml package also provides an API for this purpose:

    print(etree.tostring(root, pretty_print=True))
msg239881 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2015-04-02 07:08
duplicate of issue 14465
History
Date User Action Args
2018-08-17 15:22:53mceplsetversions: + Python 3.6, Python 3.7, Python 3.8
2015-04-02 08:20:47serhiy.storchakasetstatus: open -> closed
superseder: xml.etree.ElementTree: add feature to prettify XML output
resolution: duplicate
stage: resolved
2015-04-02 07:08:34scodersetnosy: + rhettinger
messages: + msg239881
2015-04-02 07:02:41rhettingersetnosy: + scoder, eli.bendersky, - rhettinger
2015-04-02 07:01:50rhettingercreate