Architecture of SAX API
To Parse XML using SAX API,
- First we need to create an instance of a SAXParserFactory.
- SAXParserFactory will create a SAXParser for you.
- The SAXParser will get an instance of a SAXReader which will communicate with various handlers.
- Each Handler will raise events to communicate with SAXReader to perform various tasks.
Lets explore all the Handlers one by one. We have below Handlers present in JAXP:
- ContentHandler
- ErrorHandler
- DTDHandler
- EntityResolver
So the names itself explaining about these Handlers but we will see little description of them.
ContentHandler:
This Handler contains various methods for handling content inside xml document. For this various event driven methods provided inside this Handler which invoked by a SAXReader are:-
startDocument();
endDocument();
startElement();
endElement();
characters();
processingIntructions();
These events will be invoked whenever our parser read a text inside an XML element.
ErrorHandler:
Whenever a parser encounter an error during parsing of a XML Document, ErrorHandler will basically handle those events situation by methods like:
error()
fatalError()
warning()
DTDHandler:
DTDHandler basically handles the processing of DTD (Document Type Definition) and read the document as per definition provided. What is DTD, I have already explained in my previous blog content.
EntityResolver:
Last but not least is EntityResolver. This Handler basically help to resolve an URL/URN/URI which is unique path in webspace/local to find a document and get a local copy of it.