Java XML Transform transform(final File sourceXML, final File xslt, final File targetXML)

Here you can find the source of transform(final File sourceXML, final File xslt, final File targetXML)

Description

transform

License

Open Source License

Parameter

Parameter Description
sourceXML the XML file to transform
xslt the XSL stylesheet to use for transformation
targetXML the result of XSL transformation

Exception

Parameter Description
TransformerException if an error occurs configuraing or applying the transformation

Declaration

public static void transform(final File sourceXML, final File xslt,
        final File targetXML) throws TransformerException 

Method Source Code

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

import java.io.File;

import java.util.Collections;

import java.util.Map;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    /**/*from w  w  w.j  a v a 2  s  . c om*/
     * @param sourceXML the XML file to transform
     * @param xslt the XSL stylesheet to use for transformation
     * @param targetXML the result of XSL transformation
     * @throws TransformerException if an error occurs configuraing or applying the transformation
     */
    public static void transform(final File sourceXML, final File xslt,
            final File targetXML) throws TransformerException {
        //      logger.debug("sourceXML = " + sourceXML);
        //      logger.debug("xslt = " + xslt);
        //      logger.debug("targetXML = " + targetXML);
        Transformer transformer = createTransformer(xslt,
                Collections.<String, String> emptyMap());
        Source source = new StreamSource(sourceXML);
        Result result = new StreamResult(targetXML);
        transformer.transform(source, result);
    }

    static Transformer createTransformer(final File xsltFile)
            throws TransformerException {
        return createTransformer(xsltFile,
                Collections.<String, String> emptyMap());
    }

    /**
     * @param xsltFile The XSLT stylesheet file
     * @param parms XSL parameters to pass to the stylesheet.
     * @return the configured XSLT transformer
     * @throws TransformerException if there is a problem configuring the transformer
     */
    static Transformer createTransformer(final File xsltFile,
            final Map<String, String> parms) throws TransformerException {
        try {
            Source xslSource = new StreamSource(xsltFile);
            TransformerFactory transFact = TransformerFactory.newInstance();
            transFact.setAttribute("indent-number", 2);
            Transformer transformer = transFact.newTransformer(xslSource);
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            for (String parm : parms.keySet()) {
                transformer.setParameter(parm, parms.get(parm));
            }
            return transformer;
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
            throw e;
        }
    }
}

Related

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