Java XML Document to File write(final Document doc, final File file)

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

Description

write

License

Open Source License

Declaration

public static void write(final Document doc, final File file) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2010 Oracle//from  w ww  .ja va  2 s  .  c om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Konstantin Komissarchik - initial implementation and ongoing maintenance
 ******************************************************************************/

import java.io.File;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    public static void write(final Document doc, final File file) {
        try {
            final DOMSource source = new DOMSource(doc);
            final StreamResult result = new StreamResult(file);

            final TransformerFactory factory = TransformerFactory
                    .newInstance();
            final Transformer transformer = factory.newTransformer();

            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            throw new RuntimeException(e);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }
}

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. writeDataToFile(File file, Document doc)
  6. writeDoc(Document doc, File aFile)
  7. writeDoc(Document doc, File file)
  8. writeDocToFile(Document doc, String filename)