Quantcast

How To Pretty-Print a Python ElementTree Structure

ElementTree doesn’t support pretty-printing XML. lxml does, but isn’t installed on our system. minidom‘s toprettyxml() is seriously fucked up. What to do? Turned out PyXML was installed, so I took some advice from here and came up with this function, which takes an ET node and returns a pretty-printed string:

import xml.etree.ElementTree as ET
 
from xml.dom.ext.reader import Sax2
from xml.dom.ext import PrettyPrint
from StringIO import StringIO
 
def prettyPrintET(etNode):
    reader = Sax2.Reader()
    docNode = reader.fromString(ET.tostring(etNode))
    tmpStream = StringIO()
    PrettyPrint(docNode, stream=tmpStream)
    return tmpStream.getvalue()

3 Responses to “How To Pretty-Print a Python ElementTree Structure”

  1. July 29th, 2009 | 5:37 pm

    xml.dom.ext seems completely undocumented on the web.

    http://docs.python.org/library/xml.dom.ext gives a 404

    Here is what pydoc xml.dom.ext says about PrettyPrint():

        PrettyPrint(root, stream=<open file '<stdout>', mode 'w' at 0xb7d8c068>, encoding='UTF-8', indent='  ', preserveElements=None)
  2. Dan
    July 30th, 2009 | 9:31 am

    This is where I like the lxml module for all my Python XML handling.

    import lxml.etree as etree
    fileHandle = open(‘somefile.xml’, ‘r’)
    tree = etree.parse(fileHandle)
    etree.tostring(tree, pretty_print=True)

    Of coure, if you just have an XML file the xmllint binary on unix systems is even better.

    Dan

  3. January 1st, 2010 | 9:45 pm

    Looks like xml.dom.ext is not part of Python Standard Library. It is added by installing PyXML.
    source: http://www.xml.com/pub/a/2002/09/25/py.html

    Found a prettyprint utility in “Element Library Functions”.

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

Leave a reply