File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Example: XML + XSL = HTML Using Servlet

 
bpearce4827
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
sakthiusa
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
U r having a very nice stuff.
Could u please post the whole code.
Thanks in advance,
sakthi
 
Bill Pearce
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Can u tell me from where to get xalan.jar ?
 
Brent Worden
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to download xalan at http://xml.apache.org/

------------------
Brent Worden
http://www.Brent.Worden.org/
 
Shark Kan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx , I'll see that.
Bogdan
 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Darryl Andrew
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try moving the jar files to JREROOT\lib\ext directory.
 
Darryl Andrew
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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??
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic