Here you can find the source of writeDataToFile(File file, Document doc)
public static void writeDataToFile(File file, Document doc)
//package com.java2s; /** This file is part of Approach Avoidance Task. * * Approach Avoidance Task is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version./*from w w w .j a v a2 s.co m*/ * * Approach Avoidance Task is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Approach Avoidance Task. If not, see <http://www.gnu.org/licenses/>. * */ import org.w3c.dom.Document; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.File; public class Main { public static void writeDataToFile(File file, Document doc) { try { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file Result result = new StreamResult(file); // Write the DOM document to the file TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } } }