Refactor xmlwriter function to be more flexible and readable, also addapt code to PEP8