Writing big XML files where beginning depends on end.

Magnus Lycka lycka at carmen.se
Thu Nov 24 17:06:32 CET 2005

Ben Sizer wrote:
> output = []
> def write_tree(node):
>     output.append("</node>")
>     sum = 0
>     for child in reversed(node.children):
>         if child.type = leaf:
>             output.extend(["</leaf>", "%d" % node.value, "<leaf>"])
>             sum += node.value
>         else:
>             sum += write_tree(node)
>     output.append("<node sum=%d>" % sum)
>     return sum
> write_tree(rootNode)
> output.reverse()
> print output

This won't help if we have problems keeping the whole
structure / call graph in memory at one time. Except
for the aggregated results that appear in the beginning
of each element, the xml structure basically reflects
what's happening over time, so it seems that it would
be easy to write the xml to file a chunk at a time if
we could just fill in those values in the already
written part of the file conveniently.

More information about the Python-list mailing list