Applies a stylesheet (that receives parameters) to a given xml document. : Transform « XML « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » XML » TransformScreenshots 
Applies a stylesheet (that receives parameters) to a given xml document.
     


/**
 * 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/
 */
 
 
/**
 * This class provides a set of static methods to load and transform XML
 * documents. It supports parameter-aware stylesheets (XSLT).
 
 @author Miguel Ferreira
 
 */
 
 
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 Util{


    /**
     * 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 xsltFilenamethrows 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();
    }

}

   
    
    
    
    
  
Related examples in the same category
1.XML transformation
2.A Program That Performs XML Transformations
3.XSLT I18N
4.Use the transform.TransformerFactory plugability in the JAXP API
5.Processing XML Documents Partially
6.Set the TransformerFactory system property to generate and use translets
7.Transforming DOM Node to HTML with JAXP
8.Transformer with parameters
9.Create an XML file and attach an XSL
10.Applying XSLT Stylesheets
11.Catch TransformerException
12.Formatting an XML file using Transformer
13.Transforming an XML File with XSL into a DOM Document
14.XML input, output and transform utilities
15.XSL transformations: It applies a transformation to a set of employee recordsXSL transformations: It applies a transformation to a set of employee records
16.How to write an XML file. It saves a file describing a modern drawing in SVG format.
17.Applies a stylesheet to a given xml document.
18.Apply some indentiation to some XML.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.