Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
497
The javax.xml.transform.Transformer Class
Super Class :
java.lang.Object.
Members
Signature
Public methods
abstract void
clearParameters()
abstract ErrorListener
getErrorListener()
abstract Properties
getOutputProperties()
abstract String
getOutputProperty(String
name)
abstract Object
getParameter(String
name)
abstract URIResolver
getURIResolver()
abstract void
setErrorListener(ErrorListener
listener)
abstract void
setOutputProperties(Properties
oformat)
abstract void
setOutputProperty(String
name, String value)
abstract void
setAttribute(String
name, Object value)
abstract void
-
abstract void
setURIResolver(URIResolver
resolver)
abstract void
transform(Source
xmlSource, Result outputTarget)
Overview
This
core abstract class provides XML transformation facility through TrAX and JAXP.
This instance may then be used to process XML from a variety of sources and
write the transformation output to a variety of sinks.
An
object of this class may not be used in multiple threads running concurrently.
Different Transformers may be used concurrently by different threads.
A
Transformer may be used multiple times. Parameters and output properties are
preserved across transformations.
Method Overviews
Important methods of this class are
transform() and setParameter()
Example of transform (source,result)
transform (source,result) method is used to process
the source tree to the output result.
/**
* Method
to create a result file from source file. It copies content of source file into
* result
file. This method used java.io.File for transformation.
*/
public void
simpleTransform()
{
initializeTransformerFactory();
try{
File
sourceFile = new File(sourcefileURI);
File
resultFile = new File(resultfileURI);
StreamResult result = new StreamResult(resultFile);
StreamSource source = new StreamSource(sourceFile);
transformer = tFactory.newTransformer();
transformer.transform(source,result);
}catch(TransformerException te)
{
te.printStackTrace();
}
}
Example of setParameter(String name, Object value)
setParameter(String name, Object value) is used to add a parameter for
the transformation.
Pass a qualified name as a two-part string, the namespace URI enclosed in
curly braces ({}), followed by the local name. If the name has a null URL, the
String only contain the local name. An application can safely check for a
non-null URI by testing to see if the first character of the name is a '{'
character.
For example, if a URI and local name were obtained from an element
defined with <xyz:foo
xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, then the qualified
name would be "{http://xyz.foo.com/yada/baz.html}foo". Note that no
prefix is used.
/**
* This method uses stylesheel to create a
Transformer. Applies the stylesheet to the input
* xml source and creates result html.
*/
public void transformUsingStylesheet()
{
try{
File sourceFile = new File(sourceFileURI);
File resultFile = new File(resultFileURI);
StreamResult result = new
StreamResult(resultFile);
StreamSource source = new
StreamSource(sourceFile);