Java XML Document Transform transformDocumentAsString(Document xmlDocument, Map parameters, String xsltFilename)

Here you can find the source of transformDocumentAsString(Document xmlDocument, Map parameters, String xsltFilename)

Description

Applies a stylesheet (that receives parameters) to a given xml document.

License

BSD License

Parameter

Parameter Description
xmlDocument the xml document to be transformed
parameters the hashtable with the parameters to be passed to the stylesheet
xsltFilename the filename of the stylesheet

Exception

Parameter Description
Exception an exception

Return

the transformed xml document as a string

Declaration

public static String transformDocumentAsString(Document xmlDocument,
        Map<String, String> parameters, String xsltFilename)
        throws Exception 

Method Source Code

//package com.java2s;
/**/*w  w w  . j  a  v  a 2s .c o m*/
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */

import java.io.StringWriter;

import java.util.Map;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;

public class Main {
    /**
     * Applies a stylesheet (that receives parameters) to a given xml document.
     * The resulting XML document is converted to a string after transformation.
     * 
     * @param xmlDocument
     *            the xml document to be transformed
     * @param parameters
     *            the hashtable with the parameters to be passed to the
     *            stylesheet
     * @param xsltFilename
     *            the filename of the stylesheet
     * @return the transformed xml document as a string
     * @throws Exception
     */
    public static String transformDocumentAsString(Document xmlDocument,
            Map<String, String> parameters, String xsltFilename)
            throws Exception {

        // Generate a Transformer.
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer(new StreamSource(xsltFilename));

        // set transformation parameters
        if (parameters != null) {
            for (Map.Entry<String, String> param : parameters.entrySet()) {
                transformer.setParameter(param.getKey(), param.getValue());
            }
        }

        StringWriter stringWriter = new StringWriter();
        StreamResult streamResult = new StreamResult(stringWriter);

        // Perform the transformation.
        transformer.transform(new DOMSource(xmlDocument), streamResult);
        // Now you can get the output Node from the DOMResult.
        return stringWriter.toString();
    }

    /**
     * Applies a stylesheet to a given xml document.
     * 
     * @param xmlDocument
     *            the xml document to be transformed
     * @param xsltFilename
     *            the filename of the stylesheet
     * @return the transformed xml document
     * @throws Exception
     */
    public static String transformDocumentAsString(Document xmlDocument,
            String xsltFilename) throws Exception {
        // Generate a Transformer.
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer(new StreamSource(xsltFilename));

        StringWriter stringWriter = new StringWriter();
        StreamResult streamResult = new StreamResult(stringWriter);

        // Perform the transformation.
        transformer.transform(new DOMSource(xmlDocument), streamResult);
        // Now you can get the output Node from the DOMResult.
        return stringWriter.toString();
    }
}

Related

  1. transform(Source xsl, Document doc)
  2. transformDoc(Document doc)
  3. transformDocToFile(Document doc, File file)
  4. transformDocument(Document doc, String xsl, String targetFile)
  5. transformDocument(Document xmlDocument, String xsltFilename)
  6. transformDomDocument(Transformer transformer, Node node, OutputStream os)
  7. transformXmlToString(Document importPackageDocument)
  8. transformXmlToString(Document payload)
  9. transformXMLtoString(Document xmldoc)