• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XSL transformation in a servlet error

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so this may be simple but I need some help. I'm new to Java/XML. I'm working on a servlet that simply reads in an xml file and applies the transformation. The error that I'm getting is as follows

javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: java.lang.ClassCastException: org.apache.xpath.functions.FuncQname

the code is as below

// Output goes in the response stream.
PrintWriter out = response.getWriter();
try
{

String ctx = getServletContext().getRealPath("") + FS;
String DOC_XSL = "birds.xsl";
String DOC_XML = "birds.xml";

TransformerFactory tFactory = TransformerFactory.newInstance();


if(tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE)){

DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
dFactory.setNamespaceAware(true);
DocumentBuilder db = dFactory.newDocumentBuilder();

Document xslDoc = db.parse(DOC_XSL);
Document xmlDoc = db.parse(DOC_XML);

DOMSource xslDomSource = new DOMSource(xslDoc);
xslDomSource.setSystemId("birds.xsl");
DOMSource xmlDomSource = new DOMSource(xmlDoc);
xmlDomSource.setSystemId(DOC_XML);

out.println(ctx+"<br>1<br>");
//error here
Transformer transformer = tFactory.newTransformer(xslDomSource);
out.println("after transformer creation");
transformer.transform(xmlDomSource, new StreamResult(out));
}
out.println("step4");

}
catch(javax.xml.transform.TransformerConfigurationException tce){
out.println(tce.getMessage());
out.println(tce.getCause());
}

catch (Exception e)
{
out.write(e.getMessage());
e.printStackTrace(out);
}
out.close();
}



Basically. The servlet is bombing where I've marked //error here. If anyone has an idea please help.

Thanks
 
John Lindo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some further digging reveals that Dom isn't supported by my setup of Tomcat(4.0.6). However I have Xalan and xerces 2.6.0 installed and included in the classpath when I compile.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that Tomcat does NOT use your CLASSPATH when running so there are plenty of possible library mixups. The Java standard library has included plenty of XML capability since 1.4 - are you sure you need those other libraries? Tomcat does use XML for reading configuration files so I don't understand your "Dom isn't supported" comment.

If you have to have them, see the Tomcat docs - class-loader-howto.html for where to put the Jar files so that Tomcat can find them. That file should be part of your normal Tomcat installation.
Bill
 
John Lindo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Checked out Tomcat 4 's Class Loader How-to. I also punted to using Java131. I got no love from either changes. We placed the latested xalan jar files in the WEB-INF/lib directory for the application and in the $CATALINA_BASE/shared/lib directory. Nothing has worked. Any other ideas? Was there anything wrong with the code that I posted that could be causing this issue?

Thanks,
John
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: java.lang.ClassCastException: org.apache.xpath.functions.FuncQname


If that is the error you are still getting, mismatched libraries has to be the cause. Is that version of Xalan supposed to work with Java131?
There is a lot of other odd looking code too - it looks like you are using relative file names such as "birds.xsl" that depend on the "current directory" to work - you have no control over the current directory in a servlet environment.
If this was my problem I would put the entire transform code in a separate class that could be tested outside the servlet environment first.
Bill
 
John Lindo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried separating code into a separate class. (after removing all of the servlet syntax) the code compiles and works.

And after many attempts at deleting jar files and adding them back I have proven you correct. I eliminated all of the xerces and xalan jar files except those in the WEB-INF/lib directory and everything works.

Thank you very much for your assistance.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic