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
With everything installed and configured, it's time to get your hands dirty. First up, a simple example, just to get you comfortable with how Xalan works. Consider the following XML file, snipped out from my XML-encoded address book:
Now, let's suppose I wanted to convert this snippet into the following plaintext
file:
Contact information for "Sherlock Holmes, World's Greatest Detective"
Mailing
address: 221B Baker Street, London, England
Phone: 123 6789
Email address: sherlock@holmes.domain.com
Web
site URL: http://www.method_and_madness.com/
Here's the XSLT stylesheet to get from point A to point B:
Now, we've got the XML and the XSLT. All we need is something to marry the two
together.
// import required classes
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import
java.io.*;
public class addressBookConverter {
// store the names of the
files
public static String xmlFile, xslFile, resultFile = "";
// constructor
public addressBookConverter(String xmlFile, String xslFile, String
resultFile)
{
try {
// create an instance of the TransformerFactory
// this allows the developer to use an API that is independent
of
// a particular XML processor implementation.
TransformerFactory
tFactory
= TransformerFactory.newInstance();
// create a transformer
which takes the name of the stylesheet
// as an input parameter.
// this creates a Templates object which is applied to the XML
file
// in the next step
Transformer transformer = tFactory.newTransformer(new
StreamSource(xslFile));
// transform the given XML file with the Templates object
transformer.transform(new StreamSource(xmlFile), new
StreamResult(resultFile));
System.out.println("Done!");
} catch (TransformerException e)
{
System.err.println("The following error occured: " + e);
}
}
// everything starts here
public static void main (String[]
args) {
if(args.length != 3) {
System.err.println("Please
specify three parameters:n1.
The name and path to the XML file.n2. The name and
path to the XSL
file.n3. The name of the output file.");
return;
}
// assign the parameters passed as input parameters
to
// the variables defined above
xmlFile = args[0];
xslFile
= args[1];
resultFile = args[2];
addressBookConverter myFirstExample
= new addressBookConverter
(xmlFile, xslFile, resultFile);
}
}
Now compile the class:
$ javac addressBookConverter.java
{/output]
Assuming that all goes well, you
should now have a class file named
"addressBookConverter.class". Copy this class
file to your Java
CLASSPATH, and then execute it.
[output]
$ java addressBookConverter
Ummm...Houston, we have a problem. Here's what you should see (unless you're
really intelligent and spotted me setting you up):
Please specify three parameters:
1. The name and path to the XML file.
2. The
name and path to the XSL file.
3. The name of the output file.
Let's try it again:
So, if the XML and XSL files are placed in the same folder as our application, you can use the following syntax to run the application:
Contact information for "Sherlock Holmes, World's Greatest Detective"
Mailing
address: 221B BakerStreet, London, England
Phone: 123 6789
Email address: sherlock@holmes.domain.com
Web
site URL: http://www.method_and_madness.com/