In
computing
Computing is any goal-oriented activity requiring, benefiting from, or creating computer, computing machinery. It includes the study and experimentation of algorithmic processes, and the development of both computer hardware, hardware and softw ...
, the Java API for XML Processing (JAXP) ( ), one of the
Java XML
In computing, Java XML APIs were developed by Sun Microsystems, consisting separate computer programming application programming interfaces (APIs).
Application programming interfaces
* Java API for XML Processing (JAXP)
* Java API for XML Messa ...
application programming interface
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software Interface (computing), interface, offering a service to other pieces of software. A document or standard that des ...
s (APIs), provides the capability of validating and parsing
XML
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
documents. It has three basic parsing interfaces:
* the
Document Object Model
The Document Object Model (DOM) is a cros s-platform and language-independent API that treats an HTML or XML document as a tree structure wherein each node is an object representing a part of the document. The DOM represents a document with ...
parsing interface or DOM interface
* the
Simple API for XML
SAX (Simple API for XML) is an event-driven online algorithm for lexing and parsing XML documents, with an API developed by the XML-DEV mailing list. SAX provides a mechanism for reading data from an XML document that is an alternative to tha ...
parsing interface or SAX interface
* the
Streaming API for XML or StAX interface (part of JDK 6; separate jar available for JDK 5)
In addition to the parsing interfaces, the API provides an
XSLT
XSLT (Extensible Stylesheet Language Transformations) is a language originally designed for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text, or XSL Formatting Objects. These formats c ...
interface to provide data and structural transformations on an XML document.
JAXP was developed under the
Java Community Process
The Java Community Process (JCP), established in 1998, is a formal mechanism that enables interested parties to develop standard technical specifications for Java technology. Becoming a member of the JCP requires solid knowledge of the Java program ...
as JSR 5 (JAXP 1.0), JSR 63 (JAXP 1.1 and 1.2), and JSR 206 (JAXP 1.3).
JAXP version 1.4.4 was released on September 3, 2010. JAXP 1.3 was declare
end-of-lifeon February 12, 2008.
DOM interface
The DOM interface parses an entire XML document and constructs a complete in-memory representation of the document using the classes and modeling the concepts found in the Document Object Model Level 2 Core Specification.
The DOM parser is called a , as it builds an in-memory
Document
representation. The is created by the . The creates an instance - a tree structure containing nodes in the XML Document. Each tree node in the structure implements the interface. Among the many different types of tree nodes, each representing the type of data found in an XML document, the most important include:
* element nodes that may have attributes
* text nodes representing the text found between the start and end tags of a document element.
SAX interface
The creates the SAX parser, called the . Unlike the DOM parser, the SAX parser does not create an in-memory representation of the XML document and so runs faster and uses less memory. Instead, the SAX parser informs clients of the XML document structure by invoking callbacks, that is, by invoking methods on an instance provided to the parser. This way of accessing document is called
Streaming XML.
The
DefaultHandler
class implements the , the , the , and the interfaces. Most clients will be interested in methods defined in the
ContentHandler
interface that are called when the SAX parser encounters the corresponding elements in the XML document. The most important methods in this interface are:
* and methods that are called at the start and end of a XML document.
*
startElement()
and
endElement()
methods that are called at the start and end of a document element.
*
characters()
method that is called with the text data contents contained between the start and end tags of an XML document element.
Clients provide a subclass of the
DefaultHandler
that overrides these methods and processes the data. This may involve storing the data into a database or writing it out to a stream.
During parsing, the parser may need to access external documents. It is possible to store a local cache for frequently used documents using an
XML Catalog.
This was introduced with Java 1.3 in May 2000.
[Compare th]
Java 1.2.1 API index
with th
The Java Specification Request (JSR) 5, ''XML Parsing Specification'', was finalised o
21 March, 2000
StAX interface
StAX Stax can refer to:
* StAX, (Computer Programming) Streaming API for reading and writing XML in Java
* Stax Ltd, a Japanese brand of electrostatic headphones
* Stax Records
Stax Records is an American record company, originally based in Memphis, ...
was designed as a median between the DOM and SAX interface. In its metaphor, the programmatic entry point is a cursor that represents a point within the document. The application moves the cursor forward - 'pulling' the information from the parser as it needs. This is different from an event based API - such as SAX - which 'pushes' data to the application - requiring the application to maintain state between events as necessary to keep track of location within the document.
XSLT interface
The XML Stylesheet Language for Transformations, or
XSLT
XSLT (Extensible Stylesheet Language Transformations) is a language originally designed for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text, or XSL Formatting Objects. These formats c ...
, allows for conversion of an XML document into other forms of data. JAXP provides interfaces in package allowing applications to invoke an XSLT transformation. This interface was originally called TrAX (Transformation API for XML), and was developed by an informal collaboration between the developers of a number of Java XSLT processors.
Main features of the interface are
* a factory class allowing the application to select dynamically which XSLT processor it wishes to use (, , .
* methods on the factory class to create a object, representing the compiled form of a stylesheet. This is a thread-safe object that can be used repeatedly, in series or in parallel, to apply the same stylesheet to multiple source documents (or to the same source document with different parameters) (), also , ), a method on the object to create a , representing the executable form of a stylesheet () This cannot be shared across threads, though it is serially reusable. The provides methods to set stylesheet parameters and serialization options (for example, whether output should be indented), and a method to actually run the transformation. ().
Two abstract interfaces and are defined to represent the input and output of the transformation. This is a somewhat unconventional use of Java interfaces, since there is no expectation that a processor will accept any class that implements the interface - each processor can choose which kinds of or it is prepared to handle. In practice all JAXP processors supports several standard kinds of Source (, ) and several standard kinds of Result (, ) and possibly other implementations of their own.
Example
The most primitive but complete example of XSLT transformation launching may look like this:
/* file src/examples/xslt/XsltDemo.java */
package examples.xslt;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class XsltDemo
It applies the following hardcoded XSLT transformation:
world
To the following hardcoded XML document:
The result of execution will be
hello world
Citations
References
*
External links
JAXP Reference Implementation Project Home Page
{{Jakarta EE
Java API for XML
XML Processing
XML parsers