Java XML Transform transform(File xslt_source, File xml_source)

Here you can find the source of transform(File xslt_source, File xml_source)

Description

Transforms the XML using an XSLT.

License

Open Source License

Parameter

Parameter Description
xslt_source The XSLT source.
xml_source The XML to transform.

Exception

Parameter Description
TransformerException an exception

Return

The processed result XML.

Declaration

public static String transform(File xslt_source, File xml_source) throws TransformerException 

Method Source Code


//package com.java2s;
/*/* w  ww .j  a v a 2s. co  m*/
 * utils-java8
 * Copyright (C) 2014 Raffaele Ragni
 *
 * 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;

import javax.xml.transform.sax.SAXTransformerFactory;

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

public class Main {
    private static SAXTransformerFactory tf = null;

    /**
     * Transforms the XML using an XSLT.
     *
     * @param xslt_source The XSLT source.
     * @param xml_source The XML to transform.
     *
     * @return The processed result XML.
     *
     * @throws TransformerException
     */
    public static String transform(File xslt_source, File xml_source) throws TransformerException {
        return transform(new StreamSource(xslt_source), new StreamSource(xml_source));
    }

    /**
     * Transforms the XML using an XSLT.
     *
     * @param xslt_source The XSLT source.
     * @param xml_source The XML to transform.
     *
     * @return The processed result XML.
     *
     * @throws TransformerException
     */
    public static String transform(File xslt_source, String xml_source) throws TransformerException {
        return transform(new StreamSource(xslt_source), new StreamSource(new StringReader(xml_source)));
    }

    /**
     * Transforms the XML using an XSLT.
     *
     * @param xslt_source The XSLT source.
     * @param xml_source The XML to transform.
     *
     * @return The processed result XML.
     *
     * @throws TransformerException
     */
    public static String transform(String xslt_source, String xml_source) throws TransformerException {
        return transform(new StreamSource(new StringReader(xslt_source)),
                new StreamSource(new StringReader(xml_source)));
    }

    /**
     * Transforms the XML using an XSLT.
     *
     * @param xslt_source The XSLT source.
     * @param xml_source The XML to transform.
     *
     * @return The processed result XML.
     *
     * @throws TransformerException
     */
    public static String transform(StreamSource xslt_source, StreamSource xml_source) throws TransformerException {
        Transformer t = tf.newTransformer(xslt_source);
        StringWriter result = new StringWriter();
        t.transform(xml_source, new StreamResult(result));
        return result.toString();
    }
}

Related

  1. newTransformerHandler(final SAXTransformerFactory tf)
  2. serializeXML(Transformer transformer, Source input)
  3. transform(byte[] doc, URL xsltUrl, OutputStream out)
  4. transform(byte[] source, InputStream xslInputStream, File output)
  5. transform(File inputFile, Transformer transformer, Writer out)
  6. transform(final File sourceXML, final File xslt, final File targetXML)
  7. transform(final Source xml, final Source xslt, final Map params, final Writer err)
  8. transform(InputStream styleSheet, InputStream xml, Writer out)
  9. transform(InputStream styleSheet, InputStream xml, Writer out)