Here you can find the source of transform(final File sourceXML, final File xslt, final File targetXML)
Parameter | Description |
---|---|
sourceXML | the XML file to transform |
xslt | the XSL stylesheet to use for transformation |
targetXML | the result of XSL transformation |
Parameter | Description |
---|---|
TransformerException | if an error occurs configuraing or applying the transformation |
public static void transform(final File sourceXML, final File xslt, final File targetXML) throws TransformerException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.util.Collections; import java.util.Map; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; 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 { /**/*from w w w.j a v a 2 s . c om*/ * @param sourceXML the XML file to transform * @param xslt the XSL stylesheet to use for transformation * @param targetXML the result of XSL transformation * @throws TransformerException if an error occurs configuraing or applying the transformation */ public static void transform(final File sourceXML, final File xslt, final File targetXML) throws TransformerException { // logger.debug("sourceXML = " + sourceXML); // logger.debug("xslt = " + xslt); // logger.debug("targetXML = " + targetXML); Transformer transformer = createTransformer(xslt, Collections.<String, String> emptyMap()); Source source = new StreamSource(sourceXML); Result result = new StreamResult(targetXML); transformer.transform(source, result); } static Transformer createTransformer(final File xsltFile) throws TransformerException { return createTransformer(xsltFile, Collections.<String, String> emptyMap()); } /** * @param xsltFile The XSLT stylesheet file * @param parms XSL parameters to pass to the stylesheet. * @return the configured XSLT transformer * @throws TransformerException if there is a problem configuring the transformer */ static Transformer createTransformer(final File xsltFile, final Map<String, String> parms) throws TransformerException { try { Source xslSource = new StreamSource(xsltFile); TransformerFactory transFact = TransformerFactory.newInstance(); transFact.setAttribute("indent-number", 2); Transformer transformer = transFact.newTransformer(xslSource); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); for (String parm : parms.keySet()) { transformer.setParameter(parm, parms.get(parm)); } return transformer; } catch (TransformerConfigurationException e) { e.printStackTrace(); throw e; } } }