Java API for XML Processing
In computing, the Java API for XML Processing (JAXP) (/ˈdʒækspiː/ JAKS-pee), one of the Java XML application programming interfaces (APIs), provides the capability of validating and parsing XML documents. It has three basic parsing interfaces:
In addition to the parsing interfaces, the API provides an XSLT interface to provide data and structural transformations on an XML document. JAXP was developed under the Java Community Process 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 declared end-of-life[permanent dead link ] on February 12, 2008. DOM interfaceThe 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
SAX interfaceThe The
Clients provide a subclass of the 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.[3] StAX interfaceStAX 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 interfaceThe XML Stylesheet Language for Transformations, or XSLT, allows for conversion of an XML document into other forms of data. JAXP provides interfaces in package Main features of the interface are
Two abstract interfaces ExampleThe 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 {
public static void main(String[] args) throws TransformerFactoryConfigurationError, TransformerException {
//language=xslt
String xsltResource =
"""
<?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>
""";
//language=XML
String xmlSourceResource =
"""
<?xml version='1.0' encoding='UTF-8'?>
<root><node val='hello'/></root>
""";
StringWriter xmlResultResource = new StringWriter();
Transformer xmlTransformer = TransformerFactory.newInstance().newTransformer(
new StreamSource(new StringReader(xsltResource))
);
xmlTransformer.transform(
new StreamSource(new StringReader(xmlSourceResource)), new StreamResult(xmlResultResource)
);
System.out.println(xmlResultResource.getBuffer().toString());
}
}
It applies the following hardcoded XSLT transformation: <?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>
To the following hardcoded XML document: <?xml version='1.0' encoding='UTF-8'?>
<root><node val='hello'/></root>
The result of execution will be <?xml version="1.0" encoding="UTF-8"?><reRoot><reNode>hello world</reNode></reRoot>
Citations
References
External links
|