Java XML Document to File writeDocument(Document doc, String filePath)

Here you can find the source of writeDocument(Document doc, String filePath)

Description

write Document

License

Apache License

Declaration

public static void writeDocument(Document doc, String filePath) throws IOException, TransformerException 

Method Source Code

//package com.java2s;
/**************************************************************************
 * Copyright ? 2017 Radu Simionescu//w  w  w.  java2s .c om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *************************************************************************/

import org.w3c.dom.Document;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public static void writeDocument(Document doc, String filePath) throws IOException, TransformerException {
        Path pathToFile = Paths.get(filePath);
        Files.createDirectories(pathToFile.getParent());
        writeDocument(doc, new FileOutputStream(filePath));
    }

    public static void writeDocument(Document doc, OutputStream stream) throws TransformerException, IOException {
        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer serializer;

        serializer = tfactory.newTransformer();
        //Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        serializer.transform(new DOMSource(doc), new StreamResult(stream));
        stream.close();
    }
}

Related

  1. writeDoc(Document doc, File aFile)
  2. writeDoc(Document doc, File file)
  3. writeDocToFile(Document doc, String filename)
  4. writeDocToFile(Document doc, String fileName)
  5. writeDocument(Document doc, String filename)
  6. writeDocument(Document doc, String filePath)
  7. writeDocument(Document document, File file)
  8. writeDocument(Document document, File file)
  9. writeDocument(Document document, String path)