Better still, get rid of the wasteful process where you generate XML to a
String and then write it to a file (with the wrong encoding by the way). Just generate the XML directly to the file.
(This also removes about ten lines of code from your program. But beginners always write too much code...)