Java XML Document to File writeDataToFile(File file, Document doc)

Here you can find the source of writeDataToFile(File file, Document doc)

Description

write Data To File

License

Open Source License

Declaration

public static void writeDataToFile(File file, Document doc) 

Method Source Code

//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();
        }
    }
}

Related

  1. dumpDocument(Document doc, String outName)
  2. storeXML(Document doc, File file)
  3. storeXml(final File xmlFile, final Document document)
  4. write(Document doc, File out)
  5. write(final Document doc, final File file)
  6. writeDoc(Document doc, File aFile)
  7. writeDoc(Document doc, File file)
  8. writeDocToFile(Document doc, String filename)
  9. writeDocToFile(Document doc, String fileName)