Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* gvSIG. Geographic Information System of the Valencian Government
 *
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
 * of the Valencian Government (CIT)
 * 
 * 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.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, 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 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 org.w3c.dom.Document;

public class Main {
    /**
     * Writes a DOM document in a file with XML format
     * 
     * @param doc
     *            The document to write
     * @param file
     *            The name of the output file
     * 
     * @throws FileNotFoundException
     *             java.io.FileNotFoundException
     * @throws TransformerException
     *             javax.xml.transform.TransformerException
     */
    public static void write_DOM_into_an_XML_file(Document doc, String file)
            throws FileNotFoundException, TransformerException {
        // An instance of a object transformer factory to create 'transformer'
        // objects
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();

        // Holds a tree with the information of a document in the form of a DOM
        // tree
        DOMSource source = new DOMSource(doc);

        /*
         * Creates a StreamResult object associated to a file, transforms the
         * document tree to XML and stores it in a file
         */
        // Creates the output file
        File newXML = new File(file);

        // Associates the file to a file output stream
        FileOutputStream os = new FileOutputStream(newXML);

        // Associates that stream as 'stream result' object
        StreamResult result = new StreamResult(os);

        // Makes the stream transformation from the source to the result
        transformer.transform(source, result);
    }
}