Java XML Transform transformXML(Reader xml, Reader xsl)

Here you can find the source of transformXML(Reader xml, Reader xsl)

Description

transforms an xml document using the specified xsl stylesheet

License

Open Source License

Parameter

Parameter Description
xml the document to transform
xsl the stylesheet to use

Declaration

public static String transformXML(Reader xml, Reader xsl)
        throws TransformerException 

Method Source Code

//package com.java2s;
/*//from  ww w  .j  a  v  a  2  s  .co  m
 * Copyright (c) 2003-2010 The Regents of the University of California.
 * All rights reserved.
 *
 * '$Author: crawl $'
 * '$Date: 2013-02-21 11:20:05 -0800 (Thu, 21 Feb 2013) $' 
 * '$Revision: 31474 $'
 * 
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the above
 * copyright notice and the following two paragraphs appear in all copies
 * of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 * ENHANCEMENTS, OR MODIFICATIONS.
 *
 */

import java.io.Reader;

import java.io.StringWriter;

import javax.xml.transform.Source;
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 {
    /**
     * transforms an xml document using the specified xsl stylesheet
     * 
     * @param xml
     *            the document to transform
     * @param xsl
     *            the stylesheet to use
     */
    public static String transformXML(Reader xml, Reader xsl)
            throws TransformerException {
        if (xsl != null) {
            TransformerFactory tFactory = TransformerFactory.newInstance();
            // Get the XML input document and the stylesheet, both in the
            // servlet
            // engine document directory.
            Source xmlSource = new StreamSource(xml);
            Source xslSource = new StreamSource(xsl);
            // Generate the transformer.
            Transformer transformer = tFactory.newTransformer(xslSource);
            // Perform the transformation, sending the output to the response.
            StringWriter outputWriter;
            transformer.transform(xmlSource, new StreamResult(
                    outputWriter = new StringWriter()));
            return outputWriter.getBuffer().toString();
        }

        throw new TransformerException("XSL stylesheet is null.");
    }
}

Related

  1. transformToHTML(StreamSource xmlStreamSource)
  2. transformToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
  3. transformToString(Source xmlSource, Source xslSource)
  4. transformViaXslt(String xml, String xslFile)
  5. transformXml(final StreamSource xslSrc, final StreamSource docSrc, final Map params, final URIResolver resolver)
  6. transformXmlByXslt(StreamSource source, URI xslFile)
  7. write_plain_content_tag(String name, String content, AttributesImpl atts, TransformerHandler hd)
  8. writeByTransformer(Node node, Writer writer, int indent)