TransformerFactory transFactory=TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = transFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
DOMSource source=new DOMSource();
source.setNode(doc);
StreamResult result=new StreamResult();
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
result.setOutputStream(fileOutputStream);
transformer.transform(source, result);
fileOutputStream.close();