• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Example: XML + XSL = HTML Using Servlet

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
It took me a while to find the right way of doing this, and I thought I would share it since it has saved me quite a lot of time and effort. To transform XML to HTML using XSL using a servlet is surprisingly simple. (You need xalan.jar in your path):
(1) import org.apache.xalan.xslt.*;
(2) In your service method:
String sheet = "myStyleSheet.xsl";
String doc = "myXML.xml";
XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
processor.process(new XSLTInputSource(new FileInputStream(doc)), new XSLTInputSource(new FileInputStream(sheet)), new XSLTResultTarget(res.getOutputStream()));
I have left out the try/catch block to save space. This example processes a XML file and stylesheet which are both files, but you can just as easily process using InputStreams from a URL, or some other source. In my application, I call a URL of another servelt on the same server, which returns back a dynamic XML document each time, based un URL parameters.
Have fun!
Bill
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
U r having a very nice stuff.
Could u please post the whole code.
Thanks in advance,
sakthi
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As requested, here is my complete service method for using a URL to another servlet as the source of your XML document:

Hope this helps,
Bill
[This message has been edited by Bill Pearce (edited September 19, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can u tell me from where to get xalan.jar ?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to download xalan at http://xml.apache.org/

------------------
Brent Worden
http://www.Brent.Worden.org/
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks,
I was wondering where cand I find a full example of using servlets for processing XML files to transform them in HTML with XSL.
I want to implement a catalog with books in XML and I want a servlet to read that XML file and put on the web the HTML file with XSL. I downloaded xalan form apache but their example about servlets doesn't show the right things to do what I want.In fact it's a no use example.
Can you help me?
Thank you,
Bogdan
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bogdan,
Checkout the Cocoon framework by Apache. It is an integrated framework that has a servlet engine, a parser and an XSLT engine that seamlessly transform an XML into HTML formats.
Good luck,
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Shark Kan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx , I'll see that.
Bogdan
 
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sakthiusa , Welcome to javaranch.
PROPER NAMES ARE NOW REQUIRED
Please check out the official naming policy of javaranch. Please reregister yourself with proper first & last name & help maintain the decorum of the forum.

Your Friendly Bartender
Shailesh
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bill Pearce:
[B]As requested, here is my complete service method for using a URL to another servlet as the source of your XML document:

Hope this helps,
Bill
[This message has been edited by Bill Pearce (edited September 19, 2000).][/B]


Hi, i have no problem when i compiled the java program but encountered the below error when using my browser to view the servlet.
Error: java.lang.ClassNotFoundException: org.apache.xalan.xslt.XSLTProcessorFactory
PLs help. I have already set my classpath to xalan.jar and xerces.jar.
I'm using Xalan on manta-Linus. Thanx.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try moving the jar files to JREROOT\lib\ext directory.
 
Darryl Andrew
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ajith Kallambella:
Try moving the jar files to JREROOT\lib\ext directory.


Hi, thanx for the reply. I have solved the problem by specifying the CLASSPATH in the tomcat conf files, tomcat.sh or jspc.sh

 
Darryl Andrew
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
looks like the whole bunch of code do not work in xalan2 anymore.
Anybody has any luck working with xalan2 using TransformerFactory??
 
reply
    Bookmark Topic Watch Topic
  • New Topic