Java XML Document to Stream writeXHTML(Document htmldoc, OutputStream out)

Here you can find the source of writeXHTML(Document htmldoc, OutputStream out)

Description

Write an HTML document to an output stream.

License

Open Source License

Parameter

Parameter Description
htmldoc Document to output
out Stream to write to

Exception

Parameter Description
IOException thrown on IO errors

Declaration

public static void writeXHTML(Document htmldoc, OutputStream out) throws IOException 

Method Source Code


//package com.java2s;
/*/*w  w  w  .  j av a  2s  .  c  om*/
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures
    
 Copyright (C) 2016
 Ludwig-Maximilians-Universit?t M?nchen
 Lehr- und Forschungseinheit f?r Datenbanksysteme
 ELKI Development Team
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 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 Affero General Public License for more details.
    
 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.io.OutputStream;
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 org.w3c.dom.Document;

public class Main {
    /**
     * XHTML PUBLIC doctype
     */
    public static final String HTML_XHTML_TRANSITIONAL_DOCTYPE_PUBLIC = "-//W3C//DTD XHTML 1.0 Transitional//EN";
    /**
     * XHTML SYSTEM doctype
     */
    public static final String HTML_XHTML_TRANSITIONAL_DOCTYPE_SYSTEM = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";

    /**
     * Write an HTML document to an output stream.
     * 
     * @param htmldoc Document to output
     * @param out Stream to write to
     * @throws IOException thrown on IO errors
     */
    public static void writeXHTML(Document htmldoc, OutputStream out) throws IOException {
        javax.xml.transform.Result result = new StreamResult(out);
        // Use a transformer for pretty printing
        Transformer xformer;
        try {
            xformer = TransformerFactory.newInstance().newTransformer();
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
            // TODO: ensure the "meta" tag doesn't claim a different encoding!
            xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            xformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, HTML_XHTML_TRANSITIONAL_DOCTYPE_PUBLIC);
            xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, HTML_XHTML_TRANSITIONAL_DOCTYPE_SYSTEM);
            xformer.transform(new DOMSource(htmldoc), result);
        } catch (TransformerException e1) {
            throw new IOException(e1);
        }
        out.flush();
    }
}

Related

  1. writeDocument(OutputStream stream, Document document)
  2. writeDOMDocumentToStream(Document doc, OutputStream stream)
  3. writeOutDocument(OutputStream os, Document doc)
  4. writeTransformedXml(Document doc, OutputStream output, InputStream style)
  5. writeXerces(Document doc, OutputStream out, String encoding)
  6. writeXML(Document d, OutputStream os, String sysID)
  7. writeXML(Document d, OutputStream out)
  8. writeXML(Document doc, OutputStream os)
  9. writeXML(Document doc, OutputStream os)