Java XML String Transform xslString(final String xmlFile, final InputStream xslStream)

Here you can find the source of xslString(final String xmlFile, final InputStream xslStream)

Description

xsl String

License

Open Source License

Declaration

private static String xslString(final String xmlFile, final InputStream xslStream) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;

import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    private static String xslString(final String xmlFile, final InputStream xslStream) throws Exception {
        return xslString(xmlFile, new StreamSource(xslStream));
    }/*from   ww  w .j ava 2  s.co  m*/

    private static String xslString(final String xml, final Source xslSource) throws Exception {
        TransformerFactory tFactory = TransformerFactory.newInstance();

        // 2. Use the TransformerFactory to process the stylesheet Source and
        //          generate a Transformer.
        Transformer transformer = tFactory.newTransformer(xslSource);

        // 3. Use the Transformer to transform an XML Source and send the
        //          output to a Result object.

        StringWriter sw = new StringWriter();
        StreamResult sr = new StreamResult(sw);
        transformer.transform(new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))), sr);

        return sw.getBuffer().toString();
    }
}

Related

  1. writeToFile(String xmlContent, String path)
  2. writeXML(Node node, String dtdFilename, String outputFileName)
  3. writeXml(OutputStream os, Node node, String encoding)
  4. writeXmlFile(Element doc, String filename)
  5. writeXmlResult(String xml, Writer w)