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 Example: XML + XSL = HTML Using Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Example: XML + XSL = HTML Using Servlet" Watch "Example: XML + XSL = HTML Using Servlet" New topic
Author

Example: XML + XSL = HTML Using Servlet

bpearce4827
Greenhorn

Joined: Jun 29, 2000
Posts: 8
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
sakthiusa
Greenhorn

Joined: Sep 13, 2000
Posts: 3
Hi,
U r having a very nice stuff.
Could u please post the whole code.
Thanks in advance,
sakthi


Sakthivel Palanivelu,<BR>SQL Star International Inc.
Bill Pearce
Ranch Hand

Joined: Sep 19, 2000
Posts: 53
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).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Can u tell me from where to get xalan.jar ?
Brent Worden
Ranch Hand

Joined: Nov 26, 2001
Posts: 50
You should be able to download xalan at http://xml.apache.org/

------------------
Brent Worden
http://www.Brent.Worden.org/


Brent Worden
Shark Kan
Greenhorn

Joined: Feb 14, 2001
Posts: 3
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
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Shark Kan
Greenhorn

Joined: Feb 14, 2001
Posts: 3
Thanx , I'll see that.
Bogdan
shailesh sonavadekar
Ranch Hand

Joined: Oct 12, 2000
Posts: 1874
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
Darryl Andrew
Greenhorn

Joined: Jun 21, 2001
Posts: 12
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

Joined: Mar 17, 2000
Posts: 5782
Try moving the jar files to JREROOT\lib\ext directory.
Darryl Andrew
Greenhorn

Joined: Jun 21, 2001
Posts: 12
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

Joined: Jun 21, 2001
Posts: 12
looks like the whole bunch of code do not work in xalan2 anymore.
Anybody has any luck working with xalan2 using TransformerFactory??
 
Don't get me started about those stupid light bulbs.
 
subject: Example: XML + XSL = HTML Using Servlet