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 org.w3c.dom.Document; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; public class Main { /** * Transforms an XML file using an XSL file and no parameters. * @param docFile the file containing the XML to transform. * @param xslFile the file containing the XSL transformation program. * @return the transformed DOM Document. */ public static Document getTransformedDocument(File docFile, File xslFile) throws Exception { String[] nullString = {}; return getTransformedDocument(new StreamSource(docFile), new StreamSource(xslFile), nullString); } /** * Transforms 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 docFile the file containing the XML to transform. * @param xslFile the file containing the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed DOM Document. */ public static Document getTransformedDocument(File docFile, File xslFile, Object[] params) throws Exception { return getTransformedDocument(new StreamSource(docFile), new StreamSource(xslFile), params); } /** * Transforms 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 xslFile the file containing the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed DOM Document. */ public static Document getTransformedDocument(Document doc, File xslFile, Object[] params) throws Exception { return getTransformedDocument(new DOMSource(doc), new StreamSource(xslFile), params); } /** * Transforms 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 docFile the file containing the XML to transform. * @param xslFile the file containing the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed DOM Document. */ public static Document getTransformedDocument(File docFile, Document xslFile, Object[] params) throws Exception { return getTransformedDocument(new StreamSource(docFile), new DOMSource(xslFile), params); } /** * General method for transformation to a DOM Document. Transforms 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 Source XML document to transform. * @param xsl the Source 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(); } }