Java XML Transform transformToHTML(StreamSource xmlStreamSource)

Here you can find the source of transformToHTML(StreamSource xmlStreamSource)

Description

transform To HTML

License

Open Source License

Declaration

private static String transformToHTML(StreamSource xmlStreamSource)
            throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException 

Method Source Code

//package com.java2s;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamSource;

public class Main {
    private static final String XSLT_TO_HTML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" + "\n"
            + "<xsl:output method=\"xml\" version=\"1.0\" encoding=\"UTF-8\" indent=\"yes\" omit-xml-declaration=\"no\"/>\n"
            + "\n" + "<!-- identity template -->\n" + "<xsl:template match=\"@*|node()\">\n" + "    <xsl:copy>\n"
            + "        <xsl:apply-templates select=\"@*|node()\"/>\n" + "    </xsl:copy>\n" + "</xsl:template>\n"
            + "</xsl:stylesheet>";

    private static String transformToHTML(StreamSource xmlStreamSource)
            throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
        String htmlResult = null;

        try {/*  w  w w.ja  va  2  s. co m*/
            StringWriter writer = new StringWriter();
            StringReader xsltReader = new StringReader(XSLT_TO_HTML);
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory
                    .newTransformer(new javax.xml.transform.stream.StreamSource(xsltReader));

            transformer.transform(xmlStreamSource, new javax.xml.transform.stream.StreamResult(writer));

            htmlResult = writer.toString();
        } catch (Exception e) {
        }
        return htmlResult;
    }
}

Related

  1. transformFile(Source stylesheetFile, File input, File output)
  2. transformLogViaXSLT(String logFilePath, String xslFilePath)
  3. transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
  4. transformStringToDoc(String input)
  5. transformStringToXMLGregorianCalendar(String dateTime, String pattern)
  6. transformToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
  7. transformToString(Source xmlSource, Source xslSource)
  8. transformViaXslt(String xml, String xslFile)
  9. transformXml(final StreamSource xslSrc, final StreamSource docSrc, final Map params, final URIResolver resolver)