Java XML Document to File writeXML(Document doc, String fileName)

Here you can find the source of writeXML(Document doc, String fileName)

Description

Save a xml parser document to a file

License

Open Source License

Parameter

Parameter Description
doc - The document that contains the xml items
fileName - The file name to save the xml items

Declaration

public static void writeXML(Document doc, String fileName) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*// w  ww.ja v  a  2s . c o  m
 ********************************************************************
 ** ISFS: NCAR Integrated Surface Flux System software
 **
 ** 2016, Copyright University Corporation for Atmospheric Research
 **
 ** This program 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 2 of the License, or
 ** (at your option) any later version.
 **
 ** This program 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.
 **
 ** The LICENSE.txt file accompanying this software contains
 ** a copy of the GNU General Public License. If it is not found,
 ** write to the Free Software Foundation, Inc.,
 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **
 ********************************************************************
*/

import java.io.File;
import java.io.FileNotFoundException;

import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;

public class Main {
    /**
     * Save a xml parser document to a file
     * @param doc -    The document that contains the xml items 
     * @param fileName -  The file name to save the xml items 
     */
    public static void writeXML(Document doc, String fileName) throws FileNotFoundException, IOException {

        try {
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            if (false) {
                transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                transformer.setOutputProperty(OutputKeys.METHOD, "xml");

                DOMImplementation domImpl = doc.getImplementation();
                /*
                DocumentType doctype = domImpl.createDocumentType("doctype",
                "-//Oberon//YOUR PUBLIC DOCTYPE//EN", "YOURDTD.dtd");
                */
                DocumentType doctype = domImpl.createDocumentType("doctype", "", "");
                System.err.printf("doctype.getPublicId=%s\n", doctype.getPublicId());
                System.err.printf("doctype.getSystemId=%s\n", doctype.getSystemId());
                transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
                transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
                if (doc.getDoctype() != null) {
                    String systemValue = (new File(doc.getDoctype().getSystemId())).getName();
                    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);
                }
            }

            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new FileOutputStream(new File(fileName)));
            transformer.transform(source, result);

        } catch (TransformerConfigurationException tce) {
            System.out.println("* Transformer Factory error");
            System.out.println(" " + tce.getMessage());

            Throwable x = tce;
            if (tce.getException() != null)
                x = tce.getException();
            x.printStackTrace();
        } catch (TransformerException te) {
            System.out.println("* Transformation error");
            System.out.println(" " + te.getMessage());

            Throwable x = te;
            if (te.getException() != null)
                x = te.getException();
            x.printStackTrace();
        }

    }
}

Related

  1. writeToFile(Document doc, String filePath)
  2. writeToFile(File file, Document doc)
  3. writeXML(Document doc, File file)
  4. writeXml(Document doc, File output)
  5. writeXml(Document doc, File outputFile)
  6. writeXML(final File file, final Document doc)
  7. writeXML(String OUTPUT_XML_FILE, org.w3c.dom.Document xmlDoc)
  8. writeXMLDocument(Document doc, String filename)
  9. writeXMLDocumentToFile(Document doc, String outputFilename)