formatted xml output from ElementTree inconsistency

Matthew Thorley ruach at chpc.utah.edu
Thu Jun 23 15:26:46 EDT 2005


Greetings, perhaps someone can explain this. I get to different styles 
of formatting for xmla and xmlb when I do the following:

from elementtree import ElementTree as et

xmla = et.ElementTree('some_file.xml')
xmlb = et.Element('parent')
et.SubElement(xmlb, 'child1')
et.SubElement(xmlb, 'child2')

root = et.Element('root')
root.append(xmla.getroot())
root.append(xmlb)

print et.tostring(root)

The output I get shows xmla as nicely formatted text, with elements on 
different lines and everything all tabbed and pretty. Inverly, xmlb is 
one long string on one line.

Is that because the some_file.xml is already nicely formatted? I thought 
that the formatting was ignored when creating new elements.

Is their a function to 'pretty print' an element? I looked in api ref 
and didn't see anything that would do it. It would be nice if their was 
a way to create 'standard' formatted output for all elements regardless 
of how they were created.

Comments and suggestions are greatly appreciated.

regards
-Matthew



More information about the Python-list mailing list