If you've been following along, you already know how to parse XML documents using both SAX and the DOM with the Java-based Xerces XML parser. But why stop there? In this article, take your Java/XML skills to the next level by converting your XML into other formats with the very powerful Xalan XSLT engine
Now, if I've done my job right, you should have a fairly clear idea of how to perform XSLT transformation at the command prompt. How about the doing the same in a Web environment?
Have no fear. The process is very similar, and will cause you no heartburn whatsoever. Let's take the very first example in the article and update it to work with the Web. Here's the XML:
// imported java classes
import org.xml.sax.SAXException;
import javax.xml.transform.*;
import
javax.xml.transform.stream.*;
import java.io.*;
public class addressBookHTMLConverter
{
// store the names of the files
public static String xmlFile = "";
// a Writer object to communicate with JSP
private Writer out;
// parameters
for the getAssociatedStylesheet() method of the
TransformerFactory class
//
Set them to null as they are not essential here
String media = null , title
= null, charset = null;
// constructor
public addressBookHTMLConverter(String
xmlFile, Writer out) throws
SAXException {
try {
this.out
= out;
// create an instance of the TransformerFactory
TransformerFactory tFactory =
TransformerFactory.newInstance();
// get the name of the stylesheet that has been defined
in the XML file.
// create a Source object for use by the upcoming
newTransformer() method
Source stylesheet = tFactory.getAssociatedStylesheet
(new StreamSource(xmlFile),media,
title, charset);
// create a transformer which takes
the name of the
stylesheet
// from the XML document
transformer.transform(new
StreamSource(xmlFile),new
StreamResult(out));
out.flush();
} catch (TransformerException e) {
throw new
SAXException(e);
} catch (IOException e) {
throw new SAXException(e);
}
}
}
The key difference between this example and the earlier application is the introduction
of a new Writer object, which makes it possible to redirect output to the browser instead of the standard output device.
private Writer out;
The constructor also needs to be modified to work with the Writer object:
Finally, the transform() method of the Transformer object needs to know that,
this time, output must go to the Writer object and not to a file. Note the change in the second argument to the transform() method, below.
// create a transformer which takes the name of the
stylesheet
// from the XML document
transformer.transform(new StreamSource(xmlFile),new
StreamResult(out));
Note that, since I'm using a Writer object, I will have to build some error-handling
routines into the class as well. It's instructive to examine them, and understand the reason for their inclusion.
You'll remember that I defined a Writer object at the top of my program; this Writer object provides a convenient way to output a character stream, either to a file or elsewhere. However, if the object does not initialize correctly, there is no way of communicating the error to the final JSP page.
The solution to the problem is simple: throw an exception. This exception can be captured by the JSP page and resolved appropriately. This is no different from the mechanism used when catching XML errors with the Xerces XML parser.
So that's the class. Now, we need something to tie the class to the Web server. Enter JSP.
Now, start up your Web server, with the Tomcat engine, and browse to the JSP
page. Here's what you'll see:
This example, though intended only as an illustration, should serve to demonstrate how easy it is to perform XSLT transformations in a Web environment with Xalan.