XML technologies / XSLT / Java and XSLT / JAXP API reference

(Excerpt from “Java und XSLT” by Eric M. Burke)

This page summarizes Java API for XML Processing (JAXP) version 1.1. Before transformation support, JAXP meant Java API for XML Parsing. JAXP provides Java programs with standard access to XML parsers and XSLT processors, including classes and interfaces for DOM and SAX that are not listed here.

Compared with 1.0, JAXP 1.1 adds DOM Level 2 and SAX 2 support and a new pluggability layer for transformations. It also improves the implementation-discovery algorithm for its three pluggability layers, discussed in Introduction to JAXP 1.1.

   

   

 

Package: javax.xml.parsers

These classes support parsing through Simple API for XML (SAX) 2 and Document Object Model (DOM) Level 2. They delegate actual parsing to interchangeable implementations such as Apache Crimson or Xerces.

DocumentBuilder

Instances provide an API for parsing XML from different sources and creating a new DOM object of type Document from scratch. Obtain a DocumentBuilder instance through DocumentBuilderFactory. Then method newDocument( ) creates a new DOM object of type Document without implementation-specific code.

public abstract class DocumentBuilder {
  protected DocumentBuilder( );
  public abstract DOMImplementation getDOMImplementation( );
  public abstract boolean isNamespaceAware( );
  public abstract boolean isValidating( );
  public abstract Document newDocument( );
  public Document parse(InputStream is, String systemId)
    throws SAXException, IOException;
  public Document parse(String uri)
    throws SAXException, IOException;
  public Document parse(File f)
    throws SAXException, IOException;
  public abstract Document parse(InputSource is)
    throws SAXException, IOException;
  public Document parse(InputStream is)
    throws SAXException, IOException;
  public abstract void setEntityResolver(EntityResolver er);
  public abstract void setErrorHandler(ErrorHandler eh);
}

DocumentBuilderFactory

This class creates objects of type DocumentBuilder using the Factory design pattern, isolating application code from DOM implementations. Methods configure required parser features. If unavailable, new-DocumentBuilder( ) throws ParserConfigurationException. Accessors such as isNamespaceAware( ) report configuration, not actual parser capabilities. They indicate whether the application enabled a feature for this DocumentBuilderFactory instance. First call method newInstance( ) to create the factory. Then call its method new-DocumentBuilder( ) to obtain a DocumentBuilder instance.

public abstract class DocumentBuilderFactory {
  protected DocumentBuilderFactory( );
  public abstract Object getAttribute(String name)
    throws IllegalArgumentException;
  public boolean isCoalescing( );
  public boolean isExpandEntityReferences( );
  public boolean isIgnoringComments( );
  public boolean isIgnoringElementContentWhitespace( );
  public boolean isNamespaceAware( );
  public boolean isValidating( );
  public abstract DocumentBuilder newDocumentBuilder( )
    throws ParserConfigurationException;
  public static DocumentBuilderFactory newInstance( );
  public abstract void setAttribute(String name, Object value)
    throws IllegalArgumentException;
  public void setCoalescing(boolean coalescing);
  public void setExpandEntityReferences(boolean expandEntityRef);
  public void setIgnoringComments(boolean ignoreComments);
  public void setIgnoringElementContentWhitespace(boolean whitespace);
  public void setNamespaceAware(boolean awareness);
  public void setValidating(boolean validating);
}

FactoryConfigurationError

Indicates that the parser’s Factory class could not be found or instantiated, signaling an installation problem. For the discovery algorithm, see XSLT processing with Java.

public class FactoryConfigurationError
    extends Error {
  public FactoryConfigurationError(String msg);
  public FactoryConfigurationError(Exception e);
  public FactoryConfigurationError(Exception e, String msg);
  public FactoryConfigurationError( );
  public Exception getException( );
  public String getMessage( );
}

ParserConfigurationException

The API calls this a “serious configuration error”. Usually the factory cannot provide a parser with requested features. For example, an application requests support for XML namespaces but the only parser lacks it. (XSLT requires an XML parser supporting XML namespaces.)

public class ParserConfigurationException
    extends Exception {
  public ParserConfigurationException(String msg);
  public ParserConfigurationException( );
}

SAXParser

This wraps an underlying SAX parser. Present since JAXP 1.0, it supports SAX 1 and SAX 2. Avoid methods using HandlerBase, an obsolete SAX 1 interface; prefer methods using DefaultHandler.

public abstract class SAXParser {
  protected SAXParser( );
  public abstract Parser getParser( )
    throws SAXException;
  public abstract Object getProperty(String name)
    throws SAXNotRecognizedException, SAXNotSupportedException;
  public abstract XMLReader getXMLReader( )
    throws SAXException;
  public abstract boolean isNamespaceAware( );
  public abstract boolean isValidating( );
  public void parse(InputStream is, DefaultHandler dh, String systemId)
    throws SAXException, IOException;
  public void parse(InputStream is, DefaultHandler p1)
    throws SAXException, IOException;
  public void parse(InputStream is, HandlerBase hb, String systemId)
    throws SAXException, IOException;
  public void parse(File f, HandlerBase hb)
    throws SAXException, IOException;
  public void parse(InputStream is, HandlerBase hb)
    throws SAXException, IOException;
  public void parse(String uri, HandlerBase hb)
    throws SAXException, IOException;
  public void parse(InputSource is, HandlerBase hb)
    throws SAXException, IOException;
  public void parse(InputSource is, DefaultHandler dh)
    throws SAXException, IOException;
  public void parse(String systemId, DefaultHandler dh)
    throws SAXException, IOException;
  public void parse(File f, DefaultHandler dh)
    throws SAXException, IOException;
  public abstract void setProperty(String name, Object value)
    throws SAXNotRecognizedException, SAXNotSupportedException;
}

SAXParserFactory

This factory creates SAX parsers. Before creating one, method setFeature( ) specifies required features. See saxproject.org for core SAX 2 features.

public abstract class SAXParserFactory {
  protected SAXParserFactory( );
  public abstract boolean getFeature(String name)
    throws ParserConfigurationException, SAXNotRecognizedException,
    SAXNotSupportedException;
  public boolean isNamespaceAware( );
  public boolean isValidating( );
  public static SAXParserFactory newInstance( );
  public abstract SAXParser newSAXParser( )
    throws ParserConfigurationException, SAXException;
  public abstract void setFeature(String name, boolean value)
    throws ParserConfigurationException, SAXNotRecognizedException,
    SAXNotSupportedException;
  public void setNamespaceAware(boolean awareness);
  public void setValidating(boolean validating);
}

 

Package: javax.xml.transform

This package defines a transformation API, normally for XSLT but flexible enough for other technologies. Like javax.xml.parsers, its classes and interfaces hide vendor-specific implementations. JAXP 1.1 ships with Xalan as its reference implementation, easily replaced by another processor.

ErrorListener

This interface enables custom error handling. Without a registered ErrorListener, all errors go to System.err. See Testing and debugging for details.

public interface ErrorListener {
  void error(TransformerException exception)
    throws TransformerException;
  void fatalError(TransformerException exception)
    throws TransformerException;
  void warning(TransformerException exception)
    throws TransformerException;
}

OutputKeys

OutputKeys These constants define valid output-property settings for interface Transformer and map directly to attributes of element <xsl:output>. Programmatic settings override stylesheet settings.

public class OutputKeys {
  public static final String CDATA_SECTION_ELEMENTS = "cdata-section-elements";
  public static final String DOCTYPE_PUBLIC = "doctype-public";
  public static final String DOCTYPE_SYSTEM = "doctype-system";
  public static final String ENCODING = "encoding";
  public static final String INDENT = "indent";
  public static final String MEDIA_TYPE = "media-type";
  public static final String METHOD = "method";
  public static final String OMIT_XML_DECLARATION = "omit-xml-declaration";
  public static final String STANDALONE = "standalone";
  public static final String VERSION = "version";
}

Result

This general interface describes transformation results. Implementations include DOMResult, SAXResult and StreamResult. Its two constants control escaping of XML metacharacters; see section 16.4 of the XSLT specification. The optional system ID may help with error and warning messages.

public interface Result {
  public static final String PI_DISABLE_OUTPUT_ESCAPING =
    "javax.xml.transform.disable-output-escaping";
  public static final String PI_ENABLE_OUTPUT_ESCAPING =
    "javax.xml.transform.enable-output-escaping";
  String getSystemId( );
  void setSystemId(String systemId);
}

Source

Source is a generic interface implemented by DOMSource, SAXSource and StreamSource. The system ID is particularly important for Source because it lets processors resolve relative URIs in XML and XSLT input. Such references appear in elements such as <xsl:import> and <xsl:include>.

public interface Source {
  String getSystemId( );
  void setSystemId(String systemId);
}

SourceLocator

Instances locate errors and warnings. Applications obtain instances of SourceLocator through method getLocator( ) of the TransformerException object.

public interface SourceLocator {
  int getColumnNumber( );
  int getLineNumber( );
  String getPublicId( );
  String getSystemId( );
}

Templates

These represent compiled transformation instructions. Whether a particular XSLT processor actually compiles a stylesheet is implementation-dependent. Templates objects are thread-safe and ideal for servlets: parse once and cache the stylesheet in memory as a Templates object. Output properties are a read-only representation of the stylesheet’s element <xsl:output>.

public interface Templates {
  Properties getOutputProperties( );
  Transformer newTransformer( )
    throws TransformerConfigurationException;
}

Transformer

Instances perform one or more transformations. Although Transformer objects are reusable, they are not thread-safe and cannot be used concurrently. Output-property names come from class OutputKeys and map to the stylesheet’s element <xsl:output>. Parameters instead are stylesheet parameters mapping to top-level elements <xsl:param>. Method getParameter() returns only parameters set programmatically.

public abstract class Transformer {
  protected Transformer( );
  public abstract void clearParameters( );
  public abstract ErrorListener getErrorListener( );
  public abstract Properties getOutputProperties( );
  public abstract String getOutputProperty(String name)
    throws IllegalArgumentException;
  public abstract Object getParameter(String name);
  public abstract URIResolver getURIResolver( );
  public abstract void setErrorListener(ErrorListener listener)
    throws IllegalArgumentException;
  public abstract void setOutputProperties(Properties oformat)
    throws IllegalArgumentException;
  public abstract void setOutputProperty(String name, String value)
    throws IllegalArgumentException;
  public abstract void setParameter(String name, Object value);
  public abstract void setURIResolver(URIResolver resolver);
  public abstract void transform(Source xmlSource, Result outputTarget)
    throws TransformerException;
}

TransformerConfigurationException

This signals serious problems, such as stylesheet syntax errors preventing creation of a Transformer instance. It can wrap other exceptions, such as a parser exception.

public class TransformerConfigurationException
    extends TransformerException {
  public TransformerConfigurationException(String msg);
  public TransformerConfigurationException(Throwable e);
  public TransformerConfigurationException(String msg, Throwable e);
  public TransformerConfigurationException(String msg, SourceLocator locator);
      
  public TransformerConfigurationException(String msg, SourceLocator locator,
    Throwable e);
  public TransformerConfigurationException( );
}

TransformerException

This general-purpose exception occurs during transformation. With a registered ErrorListener, processors should report exceptions there first. Otherwise they go to System.err. Message quality varies widely between processors. This class can wrap other exceptions.

public class TransformerException
    extends Exception {
  public TransformerException(String msg, Throwable e);
  public TransformerException(String msg, SourceLocator locator);
  public TransformerException(Throwable e);
  public TransformerException(String msg);
  public TransformerException(String msg, SourceLocator locator, Throwable e);
  public Throwable getCause( );
  public Throwable getException( );
  public String getLocationAsString( );
  public SourceLocator getLocator( );
  public String getMessageAndLocation( );
  public synchronized Throwable initCause(Throwable cause);
  public void printStackTrace(PrintStream ps);
  public void printStackTrace(PrintWriter pw);
  public void printStackTrace( );
  public void setLocator(SourceLocator locator);
}

TransformerFactory

This provides portable access to different instances of TransformerFactory and is the main abstraction hiding differences between vendors’ XSLT processors.

public abstract class TransformerFactory {
  protected TransformerFactory( );
  public abstract Source getAssociatedStylesheet(Source source, String media,
    String title, String charset) throws TransformerConfigurationException;
  public abstract Object getAttribute(String name)
    throws IllegalArgumentException;
  public abstract ErrorListener getErrorListener( );
  public abstract boolean getFeature(String name);
  public abstract URIResolver getURIResolver( );
  public static TransformerFactory newInstance( )
    throws TransformerFactoryConfigurationError;
  public abstract Templates newTemplates(Source source)
    throws TransformerConfigurationException;
  public abstract Transformer newTransformer(Source source)
    throws TransformerConfigurationException;
  public abstract Transformer newTransformer( )
    throws TransformerConfigurationException;
  public abstract void setAttribute(String name, Object value)
    throws IllegalArgumentException;
  public abstract void setErrorListener(ErrorListener listener)
    throws IllegalArgumentException;
  public abstract void setURIResolver(URIResolver resolver);
}

TransformerFactoryConfigurationError

Usually this means a TransformerFactory class cannot be instantiated, indicating CLASSPATH problems.

public class TransformerFactoryConfigurationError
    extends Error {
  public TransformerFactoryConfigurationError(String msg);
  public TransformerFactoryConfigurationError(Exception e);
  public TransformerFactoryConfigurationError(Exception e, String msg);
  public TransformerFactoryConfigurationError( );
  public Exception getException( );
  public String getMessage( );
}

URIResolver

JAXP normally provides an instance of URIResolver. A custom implementation lets applications control relative URI resolution in stylesheets. URIResolver, for example, determines how <xsl:include href="kopfzeilen.xslt"/> finds file kopfzeilen.xslt.

public interface URIResolver {
  Source resolve(String href, String base)
    throws TransformerException;
}

 

Package: javax.xml.transform.dom

This package supports transformations using DOM.

DOMLocator

This interface locates the DOM node where an error occurred. Since TransformerException creates instances of SourceLocator, applications must cast to obtain DOMLocator objects.

public interface DOMLocator
    extends SourceLocator {
  Node getOriginatingNode( );
}

DOMResult

This stores results in a DOM tree. With the default constructor, the processor creates a DOM node of type Document. Otherwise supply a DOM node of type Document, DocumentFragment or Element as the constructor argument.

Constant FEATURE is used with TransformerFactory.getFeature( ) to test whether the factory supports DOMResult.

public class DOMResult
    implements Result {
  public static final String FEATURE =
    "http://javax.xml.transform.dom.DOMResult/feature";
  public DOMResult(Node node);
  public DOMResult(Node node, String systemId);
  public DOMResult( );
  public Node getNode( );
  public String getSystemId( );
  public void setNode(Node node);
  public void setSystemId(String systemId);
}

DOMSource

This allows a DOM tree as input. In practice, parameter node is normally a DOM Document. Processors may also support other DOM types of Node. The system ID still matters for resolving relative URIs.

public class DOMSource
    implements Source {
  public static final String FEATURE =
    "http://javax.xml.transform.dom.DOMSource/feature";
  public DOMSource(Node node);
  public DOMSource(Node node, String systemId);
  public DOMSource( );
  public Node getNode( );
  public String getSystemId( );
  public void setNode(Node node);
  public void setSystemId(String systemId);
}

 

Package: javax.xml.transform.sax

This package supports SAX transformations. Examples appear in XSLT processing with Java.

SAXResult

This emits SAX events as transformation results. Parameter ContentHandler receives these events.

public class SAXResult
    implements Result {
  public static final String FEATURE =
    "http://javax.xml.transform.sax.SAXResult/feature";
  public SAXResult(ContentHandler handler);
  public SAXResult( );
  public ContentHandler getHandler( );
  public LexicalHandler getLexicalHandler( );
  public String getSystemId( );
  public void setHandler(ContentHandler handler);
  public void setLexicalHandler(LexicalHandler handler);
  public void setSystemId(String systemId);
}

SAXSource

This feeds SAX parser output to a processor for transformation. For object creation, the factory and resulting types are: TransformerFactory Templates or Transformer.

public class SAXSource
    implements Source {
  public static final String FEATURE =
    "http://javax.xml.transform.sax.SAXSource/feature";
  public SAXSource(XMLReader reader, InputSource inputSource);
  public SAXSource(InputSource inputSource);
  public SAXSource( );
  public InputSource getInputSource( );
  public String getSystemId( );
  public XMLReader getXMLReader( );
  public void setInputSource(InputSource inputSource);
  public void setSystemId(String systemId);
  public void setXMLReader(XMLReader reader);
  public static InputSource sourceToInputSource(Source source);
}

SAXTransformerFactory

This subclasses TransformerFactory and adds SAX-specific methods. First create an instance of TransformerFactory and cast if transFact.getFeature(SAXTransformerFactory.FEATURE) returns true.

public abstract class SAXTransformerFactory
    extends TransformerFactory {
  public static final String FEATURE =
    "http://javax.xml.transform.sax.SAXTransformerFactory/feature";
  public static final String FEATURE_XMLFILTER =
    "http://javax.xml.transform.sax.SAXTransformerFactory/feature/xmlfilter";
  protected SAXTransformerFactory( );
  public abstract TemplatesHandler newTemplatesHandler( )
    throws TransformerConfigurationException;
  public abstract TransformerHandler newTransformerHandler(Templates templates)
    throws TransformerConfigurationException;
  public abstract TransformerHandler newTransformerHandler( )
    throws TransformerConfigurationException;
  public abstract TransformerHandler newTransformerHandler(Source src)
    throws TransformerConfigurationException;
  public abstract XMLFilter newXMLFilter(Templates templates)
    throws TransformerConfigurationException;
  public abstract XMLFilter newXMLFilter(Source src)
    throws TransformerConfigurationException;
}

TemplatesHandler

This interface acts as a SAX 2 ContentHandler receiving events as a document is parsed. Afterward it returns a Templates object. Instances are created using SAXTransformerFactory.

public interface TemplatesHandler
    extends ContentHandler {
  String getSystemId( );
  Templates getTemplates( );
  void setSystemId(String systemId);
}

TransformerHandler

Instances receive SAX events and produce objects of type Transformer after parsing. Create instances using SAXTransformerFactory.

public interface TransformerHandler
    extends ContentHandler, LexicalHandler, DTDHandler {
  String getSystemId( );
  Transformer getTransformer( );
  void setResult(Result result)
    throws IllegalArgumentException;
  void setSystemId(String systemId);
}

 

Package: javax.xml.transform.stream

This package supports transformations with Java I/O streams.

StreamResult

This sends transformation results to an object of type File, Writer or OutputStream.

public class StreamResult
    implements Result {
  public static final String FEATURE =
    "http://javax.xml.transform.stream.StreamResult/feature";
  public StreamResult(OutputStream outputStream);
  public StreamResult(Writer writer);
  public StreamResult(String systemId);
  public StreamResult(File f);
  public StreamResult( );
  public OutputStream getOutputStream( );
  public String getSystemId( );
  public Writer getWriter( );
  public void setOutputStream(OutputStream outputStream);
  public void setSystemId(File f);
  public void setSystemId(String systemId);
  public void setWriter(Writer writer);
}

StreamSource

This supports input from objects of type URL, File, Reader or InputStream. The system ID resolves relative URLs in XML and XSLT input.

public class StreamSource
    implements Source {
  public static final String FEATURE =
    "http://javax.xml.transform.stream.StreamSource/feature";
  public StreamSource(InputStream inputStream);
  public StreamSource(InputStream inputStream, String systemId);
  public StreamSource(Reader reader);
  public StreamSource(Reader reader, String systemId);
  public StreamSource(String systemId);
  public StreamSource(File f);
  public StreamSource( );
  public InputStream getInputStream( );
  public String getPublicId( );
  public Reader getReader( );
  public String getSystemId( );
  public void setInputStream(InputStream inputStream);
  public void setPublicId(String systemId);
  public void setReader(Reader reader);
  public void setSystemId(File f);
  public void setSystemId(String systemId);
}

   

Back to top

 

 

 

back to top