Java Reference
In-Depth Information
Source source = new DOMSource(doc);
Result result = new StreamResult(System.out);
t.transform(source, result);
Thefirstlineinstantiatesthe
javax.xml.transform.dom.DOMSource
class,
which acts as a holder for a DOM tree rooted in the
Document
object specified
by
doc
. The second line instantiates the
javax.xml.transform.stream.StreamResult
class,whichactsasaholder
for the standard output stream, to which transformed data is sent. The third line reads
data from the
Source
instance and outputs transformed data to the
Result
instance.
Tip
AlthoughJava'sdefaulttransformerssupportthevarious
Source
and
Result
implementation classes located in the
javax.xml.transform.dom
,
javax.xml.transform.sax
,
javax.xml.transform.stax
, and
javax.xml.transform.stream
packages, a nondefault transformer (perhaps
specified via the
javax.xml.transform.TransformerFactory
system
property) might be more limited. For this reason, each
Source
and
Result
imple-
mentation class declares a
FEATURE
string constant that can be passed to
Trans-
formerFactory
's
boolean getFeature(String name)
method. This
methodreturnstruewhenthe
Source
or
Result
implementationclassissupported.
For example,
tf.getFeature(StreamSource.FEATURE)
returns true when
stream sources are supported.
The
javax.xml.transform.sax.SAXTransformerFactory
classprovides
additionalSAX-specificfactorymethodsthatyoucanuse,butonlywhenthe
Trans-
formerFactory
instance is also an instance of this class. To help you make the
determination,
SAXTransformerFactory
also declares a
FEATURE
string con-
stant that you can pass to
getFeature()
. For example,
tf.getFeature(SAXTransformerFactory.FEATURE)
returns true when
the transformer factory referenced from
tf
is an instance of
SAXTransformer-
Factory
.
MostJAXPinterfaceinstancesandthefactoriesthatreturnthemarenotthread-safe.
Thissituationalsoappliestotransformers.Althoughyoucanreusethesametransformer
multiple times on the same thread, you cannot access the transformer from multiple
threads.
Thisproblemcanbesolvedfortransformersbyusinginstancesofclassesthatimple-
mentthe
javax.xml.transform.Templates
interface.TheJavadocumentation
forthisinterfacehasthistosay:
Templates must be threadsafe for a given instance over