don t you want preservewhitespace false here true is for when you insert your own indentation whitespace nodes into the dom which i do not see  with false xmlutils toxml will do some basic formatting for you 