Java API for XML Processing
| Company type | Application programming interface |
|---|---|
| Industry | Software development |
| Products | XML parsing (DOM, SAX, StAX), XSLT transformation |
| Owner | Java Community Process |
In computing, the Java API for XML Processing (JAXP) () serves as a fundamental Java XML application programming interface (API) designed to facilitate the parsing and validation of XML documents. It supports three main parsing approaches:
- the Document Object Model parsing method (DOM)
- the Simple API for XML parsing method (SAX)
- the Streaming API for XML (StAX) method (available in JDK 6; provided as a separate jar for JDK 5)
In addition to parsing capabilities, JAXP includes an XSLT interface that allows for transformation of XML data and structure.
JAXP was developed through the Java Community Process under JSR 5 (JAXP 1.0), JSR 63 (JAXP versions 1.1 and 1.2), and JSR 206 (JAXP 1.3). It is integrated into various Java SE distributions as follows:
| Java SE version | Included JAXP version |
|---|---|
| 1.4 | 1.1 |
| 1.5 | 1.3 |
| 1.6 | 1.4 |
| 1.7.0 | 1.4.5 |
| 1.7.40 | 1.5 |
| 1.8 | 1.6 |
The release of JAXP version 1.4.4 occurred on September 3, 2010, whereas support for JAXP 1.3 officially ended on February 12, 2008.
DOM interface
The DOM interface operates by loading the entire XML document into memory, constructing a tree structure conforming to the Document Object Model Level 2 Core Specification.
This parser, known as a , builds the tree using classes from the package, instantiated through . The resulting tree consists of nodes implementing the interface, mainly including:
- element nodes which contain attributes
- text nodes representing content located between element tags
SAX interface
The SAX parser is instantiated via as a instance of . Unlike DOM, SAX does not create an in-memory representation of the document; instead, it parses documents more rapidly and with reduced memory consumption by triggering callbacks on a handler supplied by the client during processing.
Clients extend subclasses of , which implement interfaces such as ContentHandler and ErrorHandler to handle events like:
startDocument()andendDocument()startElement()andendElement()characters()for textual content
This event-driven architecture is ideal for applications that need to process or store XML data efficiently without loading the complete document into memory.
During parsing, the parser can retrieve external resources and supports caching mechanisms through XML Catalogs.
Introduced in May 2000 with Java version 1.3, SAX continues to be an essential tool for streaming XML processing within Java platforms.
StAX interface
StAX provides a hybrid solution positioned between DOM and SAX approaches. It employs a cursor-based model where the application actively "pulls" data from the parser by advancing the cursor as required, contrasting with event-based APIs like SAX that "push" data and require state management between events.
XSLT interface
XSLT (XML Stylesheet Language for Transformations) facilitates converting XML documents into alternative formats. JAXP offers interfaces (within ) to execute XSLT transformations. Initially called TrAX (Transformation API for XML), this API was created collaboratively by developers of Java XSLT processors.
Its notable capabilities include:
- A factory class enabling dynamic selection of an XSLT processor (, featuring methods such as ).
- The ability to generate a thread-safe object representing a compiled stylesheet that can be reused across various documents or threads ().
- This compiled stylesheet object can produce a , which is an executable form reusable but not thread-safe; it allows setting parameters, configuring output options like indentation, and performing transformations ().
Two abstract interfaces, and , represent transformation inputs and outputs respectively. While implementations may vary among processors, common standard types include:
- Sources: , ,
- Results: , ,
Additionally, some processors offer proprietary implementations beyond these standard types.
Example
Below is a straightforward illustration of performing an XSLT transformation using Java:<syntaxhighlight lang="java"> /* 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.stream.StreamResult; import javax.xml.transform.stream.StreamSource;
public class XsltDemo {
public static void main(String[] args) throws TransformerException {
String xsltContent =
"""
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='xml' indent='no'/>
<xsl:template match='/'>
<reRoot><reNode><xsl:value-of select='/root/node/@val' /> world</reNode></reRoot>
</xsl:template>
</xsl:stylesheet>
""";
String xmlInput =
"""
<?xml version='1.0' encoding='UTF-8'?>
<root><node val='hello'/></root>
""";
StringWriter outputWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer(
new StreamSource(new StringReader(xsltContent))
);
transformer.transform(
new StreamSource(new StringReader(xmlInput)), new StreamResult(outputWriter)
);
System.out.println(outputWriter.getBuffer().toString()); }
} </syntaxhighlight> The transformation applied by this code corresponds to the following XSLT stylesheet:<syntaxhighlight lang="xml"> <?xml version='1.0' encoding='UTF-8'?> <xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='xml' indent='no'/> <xsl:template match='/'> <reRoot><reNode><xsl:value-of select='/root/node/@val' /> world</reNode></reRoot> </xsl:template> </xsl:stylesheet> </syntaxhighlight> It is applied to this XML data:<syntaxhighlight lang="xml"> <?xml version='1.0' encoding='UTF-8'?> <root><node val='hello'/></root> </syntaxhighlight> Resulting in the following output:<syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8"?><reRoot><reNode>hello world</reNode></reRoot> </syntaxhighlight>