Stefan Behnel, 01.11.2012 22:56:
Actually, I'd love to see someone implement a magic API like this:
# open an "XMLFile" object that knows about XML serialisation
with xmlfile("somefile.xml", encoding='utf-8') as xf:
# generate an element (the root element)
with xf.Element('root-tag') as root_element:
# generate content, e.g. through iterparse
for element in generate_some_elements():
# serialise generated elements into the XML file
xf.write(element)
That looks like it should be totally trivial to do, but would make the
above use case way simpler and safer.
Thanks everyone for the comments. However, I really meant it the way I
showed above. The use case is to freely (and efficiently) mix in-memory
trees with incrementally generated content, and to safely write everything
out into a file as it is being generated.