Convert node serialization to a purely iterative algorithm.
033b319
We maintain a stack of open element nodes and non-node elements and use it to determine when to close them.