December 2002
Intermediate to advanced
288 pages
9h 46m
English
Here, we will discuss a core feature of JAXP that is underused: conversion from one API format (SAX, a stream, or DOM) to another. While this has always been one of the basic features of JAXP, it is rarely used, probably due to a lack of understanding of JAXP’s Transformation API for XML, or TRaX.
An example is the best way to illustrate this sort of format
transformation. Example 5-17 shows a helper class
that will perform these conversions for you, so you
don’t have to constantly deal with new
Transformer instances and the JAXP
TransformerFactory
.
package com.oreilly.xml; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class FormatConverter { private static boolean initialized = false; private static Transformer transformer; private static void initialize( ) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance( ); transformer = factory.newTransformer( ); initialized = true; } public static void convert(StreamSource source, SAXResult result) throws TransformerException { transformer.transform(source, ...Read now
Unlock full access