Java XML Transform transform(String xmlsource, File xsltfile)

Here you can find the source of transform(String xmlsource, File xsltfile)

Description

transform

License

Apache License

Parameter

Parameter Description
xmlsource a parameter
xsltfile a parameter

Exception

Parameter Description

Declaration

public static String transform(String xmlsource, File xsltfile) throws TransformerException 

Method Source Code

//package com.java2s;
/**/*from  ww w.j a v  a2  s  .  c  om*/
 * MediaMosa API
 *
 * A partial implementation of the MediaMosa API in Java.
 *
 * Copyright 2010 Universiteit van Amsterdam
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
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 {
    /**
     * @param xmlsource
     * @param xsltfile
     * @return
     * @throws javax.xml.transform.TransformerException
     */
    public static String transform(String xmlsource, File xsltfile) throws TransformerException {

        Source xmlSource = new StreamSource(new StringReader(xmlsource));
        Source xsltSource = new StreamSource(xsltfile);
        StringWriter stringWriter = new StringWriter();

        TransformerFactory transFact = TransformerFactory.newInstance();
        Templates cachedXSLT = transFact.newTemplates(xsltSource);
        Transformer trans = cachedXSLT.newTransformer();
        trans.transform(xmlSource, new StreamResult(stringWriter));

        return stringWriter.toString();
    }

    /**
     * @param xmlsource
     * @param xsltstream
     * @return
     * @throws javax.xml.transform.TransformerException
     */
    public static String transform(String xmlsource, InputStream xsltstream) throws TransformerException {

        Source xmlSource = new StreamSource(new StringReader(xmlsource));
        Source xsltSource = new StreamSource(xsltstream);
        StringWriter stringWriter = new StringWriter();

        TransformerFactory transFact = TransformerFactory.newInstance();
        Templates cachedXSLT = transFact.newTemplates(xsltSource);
        Transformer trans = cachedXSLT.newTransformer();
        trans.transform(xmlSource, new StreamResult(stringWriter));

        return stringWriter.toString();
    }
}

Related

  1. transform(Source xmlSource, Result outputTarget)
  2. transform(Source xmlSource, Templates template, Result result, Map parameters)
  3. transform(Source xsl, Source xml, Result result)
  4. transform(String xmlInputFile, String xsltInputFile, OutputStream outputStream)
  5. transform(String xmlInURI, String xslInURI)
  6. transform(String xslSource, Node original)
  7. transform(Transformer transformer, Node element)
  8. transformar(String xmlOrigen, String xslOrigen)
  9. transformer()