ElementTree and proper identation?

Wade Leftwich wleftwich at gmail.com
Wed Sep 27 14:18:13 EDT 2006


John Salerno wrote:
> John Salerno wrote:
> > I've been doing a little studying of ElementTree and it doesn't seem
> > very satisfactory for writing XML files that are properly
> > formatted/indented. I saw on the website that there is an
> > indent/prettyprint function, but this isn't listed in the Python docs
> > and I didn't see it after doing a dir(), so I guess it isn't a part of
> > the Python version.
> >
> > Did I miss something somewhere else, or can you just not use ElementTree
> > to write formatted XML files as they'd look if done by hand?
> >
> > Thanks.

You can roll your own by starting at the root and recursing through
child elements, increasing the indentation as you go:

But I do it the lazy way:

$ xmllint --format ugly.xml >pretty.xml

-- Wade Leftwich
Ithaca, NY




More information about the Python-list mailing list