Refactor: python: Add helper functions for building XML.
Pull the for loops out into their own functions that build attribute
strings, and then wrap those in other functions that add the element
tags. This doesn't make for shorter code (especially not with the
docstrings), but I think it makes for easier to follow code.