![](https://secure.gravatar.com/avatar/8b97b5aad24c30e4a1357b38cc39aeaa.jpg?s=120&d=mm&r=g)
Phil Mayers, 03.02.2014 23:32:
AFACIT the thing xmlfile() provides is a way to serialise a tag "as a child" of another one, which solves the namespace thing. But from my PoV it's unfortunate that it's used via a context manager - asynchronous code styles differ, but I prefer to avoid keeping stack frames around. Presumably I could drive the context manager myself, but... yuck.
It's pretty easy to map using a generator: def writer(out_stream, terminator="DONE"): with xmlfile(out_stream) as xf: with xf.element('root'): try: while True: el = (yield) xf.write(el) except GeneratorExit: pass w = writer(stream) next(w) And then, whenever you have something to write, you say w.send(el) And when done: w.close() Something along those lines. Completely untested, but if someone gets this to work nicely, I'd take doc patches. :) Stefan