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(_at_)robrohan(_dot_)com]
Sent: Friday, June 20, 2003 3:52 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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(_at_)yahoo(_dot_)de]
Sent: Friday, June 20, 2003 3:13 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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
--
´¨)
¸.·´¸.·´¨) ¸.·´¨)
(¸.·´ (¸.·´ .·´ ¨¨°º©Rob Rohan©º°¨¨°º ¨¨°º©?
http://treebeard.sourceforge.net
http://ashpool.sourceforge.net
-----BEGIN PGP P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1mp/z0f4y4urEiXPp0mp
OodBnE9z90bV0TaAv5MKd7G2dI+1e57/IAo8nI0SweZOIY5aKNHa7wADBQf/Zrg+
5YClrd8e044Aw0xHgpu1K506I3bfQBkmRPqKXMFwX2ZFL1m7lG+aHOyTwuuxteUC
SIT3bKGwWZX70COcU2bHu24Rpd6CiugK1GF0RaZHC0cKAbrrgljFCX4RVK3b+l0c
tRNaN4WLcJDdCLXy+WCIRMmAjjD7wEqSff5woLF5KDOaHw3jbz71rE4h9OEjEwcm
wUfPtA7Q2hGDCriDVnEBYN/HN4fFy5lbpV677+JaV3O5zZ5/SKG0atzLHlYQGO18
ersSWxsU61JTBKWSdT95aWKD42jXsLJmlWIfbLlpe8H9NlQqKAf+LfYE7gE3hcgS
xB55e6lOCu54qTr3lIhGBBgRAgAGBQI+5oyNAAoJEE6b6BGYNseY1FYAn1Q7yy/R
bpMbWhEAkMmlOVI6G9hqAJ9P980Z8hCooc3PKSqmZ7O/Y3E4QQ==
=zEJg
-----END PGP PUBLIC KEY BLOCK-----
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list