Using the indent method

Mark Tolonen M8R-yfto6h at mailinator.com
Wed May 14 21:54:31 EDT 2008


"dj" <d.a.abernathy at gmail.com> wrote in message 
news:ffbc75e3-612b-4195-b9c1-11df9e66d286 at f63g2000hsf.googlegroups.com...
> Hello All,
>
> I am using elementtree to write an XML document and I am having a hard
> time adding the correct indentation.
> I have tried using the indent method, but I can not figure out how to
> use it. Any suggestions.

Using the version of indent() found on 
http://effbot.org/zone/element-lib.htm:

>>> from xml.etree import ElementTree as ET
>>> x='<a><b /><c /><d>stuff</d><e><f /></e></a>'
>>> e=ET.fromstring(x)
>>> ET.dump(e)
<a><b /><c /><d>stuff</d><e><f /></e></a>
>>> indent(e)
>>> ET.dump(e)
<a>
  <b />
  <c />
  <d>stuff</d>
  <e>
    <f />
  </e>
</a>

-Mark 




More information about the Python-list mailing list