This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Invoking XALAN API with an in-memory XML/XSL string
- From: "Roger L. Costello" <costello at mitre dot org>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Thu, 18 Apr 2002 09:16:28 -0400
- Subject: Re: [xsl] Invoking XALAN API with an in-memory XML/XSL string
- Organization: The MITRE Corporation
- References: <002201c1e6b7$3b952bc0$6501a8c0@pcukmka>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Michael Kay wrote:
>
> Just give the parser the character string to play with directly:
>
> StringReader sr = new StringReader(xsl);
> StreamSource ss = new StreamSource(ss);
Thanks Michael! It works great. For those who might be interested,
here is the Java code to do transformations either in-memory, or from
files:
// This code shows how to do a transformation where the xml and
// xsl are in-memory strings
public static String doTransform(String xml, String xsl)
throws TransformerException, TransformerConfigurationException,
FileNotFoundException, IOException {
TransformerFactory tFactory = TransformerFactory.newInstance();
StringReader xsl_sr = new StringReader(xsl);
StreamSource xsl_ss = new StreamSource(xsl_sr);
Transformer transformer = tFactory.newTransformer(xsl_ss);
StringReader xml_sr = new StringReader(xml);
StreamSource xml_ss = new StreamSource(xml_sr);
StringWriter out_sw = new StringWriter();
StreamResult out_sr = new StreamResult(out_sw);
transformer.transform(xml_ss, out_sr);
return out_sw.toString();
}
// This code shows how to transformation where the xml and
// xsl are files. The result is sent to a file.
public static void doTransform(String xmlfilename,
String xslfilename,
String outfilename)
throws TransformerException, TransformerConfigurationException,
FileNotFoundException, IOException {
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer(new StreamSource(xslfilename));
transformer.transform(new StreamSource(xmlfilename),
new StreamResult(new FileOutputStream(outfilename)));
}
/Roger
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list