Clean XML serialization

This is an intermediate refactoring that uses custom methods for writing
XML into a Writer. The next refactoring step is to replace with Writer
with a SAX ContentHandler.