An XML parser is an essential package for any serious programming effort involving XML document. The XML parser is the link between the XML document representing data and the application code. There are many ways to parse XML documents with Java such as DOM and SAX which are the standard parsing techniques.
The Java API for XML Processing (JAXP) enables applications to parse and transform XML documents independent of a particular XML processing (parser) implementation. JAXP is a Java interface that provides a standard approach to parsing XML documents.
JAXP provides parsers for
JAXP adds a few factory classes to fill some holes in these APIs and enable Java programmers to write completely parser-independent code. The factory class you use determines the approach you use. A factory class is a standard design pattern that gives you the ability to manufacture classes as needed.
A basic JAXP session will look like this:
- Instantiate a factory class
- Using the factory class instantiate the provider class
- Using the provider class created in the previous step perform the XML processing/parsing
With JAXP, you can use either the DocumentBuilderFactory to create DocumentBuilder classes or the SAXParserFactory to create SAXParser classes. The difference is that DOM parsers read the entire document into memory and allow you to traverse the document in a random access way, while SAX parsers call handlers to interpret XML data as it's encountered in the document.
The one major new invention in JAXP that was not based on previous standards was a series of abstract factory classes in the javax.xml.parsers package. These allow a Java program to obtain a DOM parser, a SAX1 Parser, or a DOMImplementation in a parser-independent fashion. The SAX1 factories are now obsolete, but the DOM factories are still quite useful.