Java tutorial
//package com.java2s; /*--------------------------------------------------------------- * Copyright 2005 by the Radiological Society of North America * * This source software is released under the terms of the * RSNA Public License (http://mirc.rsna.org/rsnapubliclicense) *----------------------------------------------------------------*/ import java.io.*; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; public class Main { /** * Transform an XML file using an XSL file and an array of parameters. * The parameter array consists of a sequence of pairs of (String parametername) * followed by (Object parametervalue) in an Object[]. * @param doc the document to transform. * @param xsl the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed DOM Document. */ public static Document getTransformedDocument(File doc, File xsl, Object[] params) throws Exception { return getTransformedDocument(new StreamSource(doc), new StreamSource(xsl), params); } /** * Transform an XML DOM Document using an XSL file and an array of parameters. * The parameter array consists of a sequence of pairs of (String parametername) * followed by (Object parametervalue) in an Object[]. * @param doc the document to transform. * @param xsl the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed DOM Document. */ public static Document getTransformedDocument(Document doc, File xsl, Object[] params) throws Exception { return getTransformedDocument(new DOMSource(doc), new StreamSource(xsl), params); } /** * Transform an XML file using an XSL DOM Document and an array of parameters. * The parameter array consists of a sequence of pairs of (String parametername) * followed by (Object parametervalue) in an Object[]. * @param doc the file containing the XML to transform. * @param xsl the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed DOM Document. */ public static Document getTransformedDocument(File doc, Document xsl, Object[] params) throws Exception { return getTransformedDocument(new StreamSource(doc), new DOMSource(xsl), params); } /** * Transform an XML document using an XSL DOM document and an array of parameters. * The parameter array consists of a sequence of pairs of (String parametername) * followed by (Object parametervalue) in an Object[]. * @param doc the document to transform. * @param xsl the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed DOM Document. */ public static Document getTransformedDocument(Document doc, Document xsl, Object[] params) throws Exception { return getTransformedDocument(new DOMSource(doc), new DOMSource(xsl), params); } /** * General method for transformation to a DOM Document. Transform a Source * document using a Source XSL document and an array of parameters. * The parameter array consists of a sequence of pairs of (String parametername) * followed by (Object parametervalue) in an Object[]. * @param doc the XML document to transform. * @param xsl the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed text. */ public static Document getTransformedDocument(Source doc, Source xsl, Object[] params) throws Exception { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xsl); if ((params != null) && (params.length > 1)) { for (int i = 0; i < params.length; i = i + 2) { transformer.setParameter((String) params[i], params[i + 1]); } } DOMResult domResult = new DOMResult(); transformer.transform(doc, domResult); return (Document) domResult.getNode(); } }