Java XML Document to String write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)

Here you can find the source of write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)

Description

Transforms a memory document with XML format into another with HTML format according an XSL file, and stores it in a file

License

Open Source License

Parameter

Parameter Description
doc The document to read
htmlFile The name of the output (HTML) file
xslFile The name of the XSL file which allows transform XML into HTML according its style

Exception

Parameter Description
FileNotFoundException java.io.FileNotFoundException
TransformerException javax.xml.transform.TransformerException

Declaration

public static void write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)
        throws FileNotFoundException, TransformerException 

Method Source Code

//package com.java2s;
/* gvSIG. Geographic Information System of the Valencian Government
 *
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
 * of the Valencian Government (CIT)/* w  ww  .j ava  2s.c om*/
 * 
 * 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 javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;

public class Main {
    /**
     * Transforms a memory document with XML format into another with HTML
     * format according an XSL file, and stores it in a file
     * 
     * @param doc
     *            The document to read
     * @param htmlFile
     *            The name of the output (HTML) file
     * @param xslFile
     *            The name of the XSL file which allows transform XML into HTML
     *            according its style
     * 
     * @throws FileNotFoundException
     *             java.io.FileNotFoundException
     * @throws TransformerException
     *             javax.xml.transform.TransformerException
     */
    public static void write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)
            throws FileNotFoundException, TransformerException {
        // An instance of a object transformer factory
        TransformerFactory tFactory = TransformerFactory.newInstance();

        // Creates the output file
        File SalidaHTML = new File(htmlFile);

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

        // Creates a transformer object associated to the XSL file
        Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));

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

        // Makes the transformation from the source in XML to the output in HML
        // according the transformer in XSL
        transformer.transform(sourceId, new StreamResult(os));
    }
}

Related

  1. write(Document doc)
  2. write(Document doc, Result result)
  3. write(String filename, Document document, boolean addDocType)
  4. write2xml(Document document)
  5. write2Xml(Document document)
  6. writeDocument(Document doc, boolean omitXmlDeclaration)
  7. writeDocument(Document document)
  8. writeDocumentToStreamResult(Document document, StreamResult outputTarget)
  9. writeToBytes(Document document)