If I'm understanding you correctly, you can only get an XMLDocument, and not a string containing the xml to write to a file.
Here's what I do.
XMLDocument doc = .....
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
doc.print(baos);
String text = baos.toString();
Good Luck!
dvb