Writing big XML files where beginning depends on end.

Ben Sizer kylotan at gmail.com
Thu Nov 24 10:36:31 EST 2005


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

:)
(There is probably a cleaner way to do this than the quick hack above.)

-- 
Ben Sizer




More information about the Python-list mailing list