SAX Parser – Simple API for XML
SAX parser work differently with DOM parser.SAX parser is faster and uses less memory than DOM parser.SAX don’t load and XML document into memory and don’t create any object representation of XML document instead, SAX is use the event-driven model its provides callback method and the parser invokes them as it reads the XML data.
Using SAX parser you can’t back up to an earlier part of document or rearrange it, any more than you can back up the serial data stream or rearrange characters you have read from than stream. SAX will call listener object for following events:
- processing instructions
SAX call back methods call the DefaultHandler (org.xml.sax.helpers.DefaultHandler).You should extend the Default handler override the following methods or use DefaultHandler as Inner Class.
Methods must be overridden are
startDocument() and endDocument() – methods called at start and end of an XML document
startElement() and endElement() – methods called at the start and end of each element.
characters() – method called when text contents between start and end of element
SAX is oriented towards state independent processing, where the handing of element does not depend on the previous elements.
See the SAX Parser example for more details Read XML File using Sax Parser