File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Error transforming using TransformerHandler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Error transforming using TransformerHandler" Watch "Error transforming using TransformerHandler" New topic
Author

Error transforming using TransformerHandler

Sankar Tanguturi
Greenhorn

Joined: Feb 18, 2003
Posts: 17
Hi,

I am not sure if this is the right forum. But since my problem involves using TransformerHandlers, I am posting it here.
I am trying to port a web application that from Oracle9iAS to Websphere 5.0. I am getting a problem on Websphere, when I try to get a TransformerHandler from TransformerFactory and using TransformerHandler as a ContentHandler to fire SAX events. The transformation does not happen and I get an empty XML declaration in the output. I verified that if I do not specify the templates while getting the TransformerHandler, it is doing an identity transformation correctly. Also there are templates within my XSL to handle the XML tags.
Here is the outline of my code :
if( templates == null )
{
templates = TransformerFactory.newInstanc().newTemplates
(new StreamSource(new File("xml_to_fo.xsl")));
}
TransformerHandler hd = tf.newTransformerHandler
(templates);
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty( OutputKeys.INDENT,"no" );
hd.setResult( new StreamResult(new File(OUTPUT_FILENAME) );
hd.startDocument();
..... // Code to add XML nodes to the document
hd.endDocument();
I tried 2 different approaches to troubleshoot this:
1) On Oracle9IAS I was using an older version on xalan. I thought that the problem may due to a bug in the xalan that ships with Websphere. So I changed the Application class loading mode to 'Parent Last' from 'Parent First' so that Websphere would pick my version of the xalan classes(under web-inf/classes/lib). That did not help. In both cases, when I printed the loader info, I got com.ibm.ws.bootstrap.ExtClassLoader@56fd9395 as class loader and sun.misc.Launcher$AppClassLoader@55021395 as parent loader.
2) Next I wrote a stand alone java program to test the transformation process with the xalan.jar version from websphere and the version I had from Oracle9iAS. I found that with both versions of the jar, transformation happened correctly. I think this probably means that there is something wrong in the Websphere environment that is causing the problem. The stand alone program code is shown below :
TransformerFactory tf = TransformerFactory.newInstance();
Templates templates = tf.newTemplates(new StreamSource("xml_to_fo.xsl"));
TransformerHandler hd = ((SAXTransformerFactory)tf).newTransformerHandler
(templates );
Transformer transformer = hd.getTransformer();
transformer.transform(new StreamSource(new File("source.xml")), new StreamResult(System.out));
Any help in fixing the problem is appreciated.
Thanks,
Sankar
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You don't show any exception handling - are you sure that none are being thrown? For instance by the transform() call?
Bill
Sankar Tanguturi
Greenhorn

Joined: Feb 18, 2003
Posts: 17
I did not show the exception handling, but it exists . What I showed is an extract from a larger program.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error transforming using TransformerHandler