[XML-SIG] elementtree, element.text = value problem and a couple other questions

Fredrik Lundh fredrik at pythonware.com
Mon Dec 19 22:10:12 CET 2005


Werner F. Bruhin wrote:

> By the way is it possible to write the file in a "pretty" format?

here's a simple (and only lightly tested) in-place indenter:

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

to prettyprint, just call this before you serialize the tree.  (note that
this doesn't work well on documents that contain mixed content).

</F>





More information about the XML-SIG mailing list