The Java API for XML Processing, or JAXP, is one of the Java XML programming APIs. It provides the capability of validating and parsing XML documents. The two basic parsing interfaces are:
Table of contents |
2 SAX interface 3 XSLT interface 4 Additional Resources |
The DOM interface is perhaps the easiest to understand. It parses an entire XML document and constructs a complete in-memory representation of the document using the classes modeling the concepts found in the Document Object Model(DOM) Level 2 Core Specification.
The DOM parser is called a DocumentBuilder, as it builds an in-memory Document representation. The javax.xml.parsers.DocumentBuilder is created by the javax.xml.parsers.DocumentBuilderFactory. The DocumentBuilder creates a org.w3c.dom.Document instance, which is a tree structure containing nodes in the XML Document. Each tree node in the structure implements the org.w3c.dom.Node interface. There are many different types of tree nodes, representing the type of data found in an XML document. The most important node types are:
The SAX parser is called the SAXParser and is created by the javax.xml.parsers.SAXParserFactory. Unlike the DOM parser, the SAXParser does not create an in-memory representation of the XML document and so is faster and uses less memory. Instead, the SAXParser informs clients of the XML document structure by invoking callbacks, that is, by invoking methods on a DefaultHandler instance provided to the parser.
The DefaultHandler class is found in the org.xml.sax.helpers package. It implements the ContentHandler, the ErrorHandler, the DTDHandler, and the EntityResolver interfaces. Most clients will be interested methods defined in the ContentHandler inteface.
The ContentHandler methods implemented by the DefaultHandler are called when the SAX parser encounters the corresponding elements in the XML document. The most important methods in this interface are:
The XML Stylesheet Language for Transformations, or XSLT, allows conveting an XML document into other forms of data.DOM interface
Refer to the javadoc documentation of the package org.w3c.dom for a complete list of node types.
SAX interface
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.XSLT interface