Subject: Re: [xsl] Optimizing Trax From: "Mark R. Diggory" <mdiggory@xxxxxxxxxxxxxxxxx> Date: Tue, 24 Jun 2003 09:26:45 -0400 |
Based on the various responses I received I attempted to use SAX to transform an XML file to a SAXResult, and then to read in the SAXResult as an input source to a second transformation, and transform the document to HTML to be sent to the browser. I received an error complaining that the ContentHandler of the SAXResult was null. Obviously, I don't' understand how to set/use a ContentHandler, and I obviously don't understand how to convert a SAXResult to a SAXSource. Here is the relevant code snippet in my JSP page. Any help for a newbie like me would be greatly appreciated, as I am at my wits end. Also, if this is not the right newsgroup for this problem, please direct me to the right place.
String xsl = "/stylesheets/data.xsl";
String xsl2 = "/stylesheets/format.xsl";
Templates templates = (Templates)application.getAttribute("templates");
Templates templates2 = (Templates)application.getAttribute("templates2");
if (templates == null) {
TransformerFactory tfactory = TransformerFactory.newInstance();
templates = tfactory.newTemplates(new StreamSource(getClass().getResourceAsStream(xsl)));
application.setAttribute("templates", templates);
}
if (templates2 == null) {
TransformerFactory tfactory = TransformerFactory.newInstance();
templates2 = tfactory.newTemplates(new StreamSource(getClass().getResourceAsStream(xsl2)));
application.setAttribute("templates2", templates2);
}
Transformer transformer = templates.newTransformer();
SAXResult saxResult = new SAXResult();
transformer.transform(new StreamSource(new StringReader(hand.detailresult)), saxResult);
transformer = templates2.newTransformer();
transformer.transform(new StreamSource(saxResult.getSystemId()), new StreamResult(out));
-----Original Message-----
From: Rob Rohan [mailto:me@xxxxxxxxxxxx] Sent: Friday, June 20, 2003 3:52 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Optimizing Trax
On Fri, 2003-06-20 at 12:22, Schwartz, Rechell R, ALABS wrote:
How do I tranform the document using SAX-- i.e., how do I change this line?
transformer.transform(new DOMSource(document), new StreamResult(out));
This is kind of more a java question than an XSLT question. Here is a snip-it that I use in one of my programs - it's a touch kludgey but if you know java you can decipher the needed parts - if you can't I recommend "Java & XML" second edition by Brett McLaughlin from O'Reilly
public long transform(java.io.InputStream xmlin, java.io.InputStream
xslin, java.io.OutputStream output, java.io.File xslURI, java.io.File
xmlURI) throws Exception {
strResult = null;
if(params == null)
params = new Vector();
String media = null;
//set the factory to the specified one System.setProperty("javax.xml.transform.TransformerFactory", XSLTFactory);
//get an instance and make a result object tfactory = javax.xml.transform.TransformerFactory.newInstance();
strResult = new javax.xml.transform.stream.StreamResult(output);
stf = (javax.xml.transform.sax.SAXTransformerFactory) tfactory;
StreamSource ss = new StreamSource(xslin);
if(xslURI != null) ss.setSystemId(xslURI);
stylesheet = tfactory.newTemplates(ss);
transformer = stylesheet.newTransformer();
//set params for(int i=0; i < params.size()-1; i++)
transformer.setParameter((String) params.elementAt(i), (String)params.elementAt(i+1));
//do the transformation StreamSource input = new StreamSource(xmlin);
if(xmlURI != null) input.setSystemId(xmlURI);
transformer.transform(input, strResult);
....
Rechell Schwartz
-----Original Message----- From: J.Pietschmann [mailto:j3322ptm@xxxxxxxx] Sent: Friday, June 20, 2003 3:13 PM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] Optimizing Trax
Schwartz, Rechell R, ALABS wrote:
I am using Trax to transform my xml files, and am finding the
performance
very slow especially for large files. Are there any ways of optimizing
the
performance such as by pre-compiling the stylesheets?
Yes, but your time is probably wasted elsewhere.
> transformer = tFactory.newTransformer(new StreamSource(getClass().getResourceAsStream(xsl)));
> transformer.transform(new DOMSource(document), new
StreamResult(out));
^^^^^^^^^^^^^^^^
Use a profiler to check where your program wastes time:
- DB access or whereever you get your raw data
- DOM construction
- transformation
If it is the latter, check your XSL for inefficient constructs.
If it is DOM construction, use SAX. Also DOM uses a lot of memory,
which may be a problem in itself (more GC).
J.Pietschmann
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Optimizing Trax, Schwartz, Rechell R, | Thread | Re: [xsl] Optimizing Trax, Wendell Piez |
[xsl] background-image being enlarg, Aimee Rodgers | Date | RE: [xsl] Different Colors for Alte, Jarno . Elovirta |
Month |