May 10, 2021 Java
Here is a summary of the key SAX APIs:
Class | Usage |
---|---|
SAXParserFactory | Create an instance of the parser .xml by the system property javax and the user.parsers.SAXParserFactory. |
SAXParser | The SAXParser interface defines several overloaded parse() methods. |
SAXReader | SAXParser wraps a SAXReader and returns it from SAXParser's getXMLReader() method. |
DefaultHandler | DefaultHandler implements the Content Handler, Error Handler, DTD Handler, and EnterpriseResolver interfaces. By using DefaultHandler, we can cover only what we need. |
ContentHandler | This interface defines callback methods such as startDocument, endDocument, startElement, and endElement. T hese methods are called when identifying XML tags. I t also defines the called method acters() when the parser encounters text in an XML element. It defines the called processIngInstruction() when the parser encounters an inline processing instruction. |
ErrorHandler | It uses the error(), fatalError() and warning() methods to respond to various parsing errors. The default error handler throws only fatal errors and exceptions ignore validation errors. |
DTDHandler | Used to process DTDs |
EntityResolver | Its resolveEntity() method is used to identify data. |
We typically implement most
ContentHandler
To provide a more robust implementation, we can implement
ErrorHandler
The SAX parser is defined in the packages listed in the table below.
Package | Describe |
---|---|
org.xml.sax | Define the SAX interface. |
org.xml.sax.ext | Define sAX extensions for more advanced SAX processing. |
org.xml.sax.helpers | Define the secondary class of the SAX API. |
javax.xml.parsers | Define the SAXParserFactory class, which returns to SAXParser. |
javax.xml.parsers.DocumentBuilderFactory
returns a
DocumentBuilder
instance.
We use
DocumentBuilder
instance to produce a
Document
XML document.
The builder is determined by
javax.xml.parsers.DocumentBuilderFactory
The
newDocument()
creates
org.w3c.dom.Document
interface.
We can use the parsing method of one of the builders to create
Document
an existing XML document.
The document object model implementation defines the packages listed in the table below.
Package | Describe |
---|---|
org.w3c.dom | Define the DOM programming interface for XML documents. |
javax.xml.parsers | Define the Document Builder Factor class and the DocumentBuilder class. |
TransformerFactory
creates a
Transformer
object.
The XSLT API is defined in the package shown in the table below.
Package | Describe |
---|---|
javax.xml.transform | Define the Transformer Factor and Transformer classes. We can call the transform() method from the transformer object to transform it. |
javax.xml.transform.dom | The class used to create input and output objects from the DOM. |
javax.xml.transform.sax | A class used to create input objects from the SAX parser and output objects from the SAX event handler. |
javax.xml.transform.stream | The class used to create input and output objects from the I/O stream. |
StAX provides developers with an alternative to SAX and DOM parsers.
StAX can filter, process, and modify high-performance streams with less memory.
StAX is a standard two-way pull parser interface for streaming XML processing.
StAX provides a simpler programming model than SAX and is more memory efficient than the DOM.
StAX can parse and modify the XML stream as an event.
StAX APIs are defined in the packages shown in the table below.
Package | Describe |
---|---|
javax.xml.stream | Defines the XMLStreamReader interface for iterative XML document elements. /b10> Define the XMLStreamWriter interface and specify how to write XML. |
javax.xml.transform.stax | Provides a StAX-specific conversion API. |