Wednesday, 16 December 2015

SAX(Simple API for XML Parsing)


Architecture of SAX API





To Parse XML using SAX API,
  1. First we need to create an instance of a SAXParserFactory.
  2. SAXParserFactory will create a SAXParser for you.
  3. The SAXParser will get an instance of a SAXReader which will communicate with various handlers.
  4. 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:

  1. ContentHandler
  2. ErrorHandler
  3. DTDHandler
  4. 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.

No comments:

Post a Comment