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.

Sunday, 13 December 2015

Basics of JAXP


So far we have seen the basics of parsers like SAX and DOM. Now another most terrifying term in XML is JAXP. Need not to worry because you have already seen some part of it. Surprising!! How? Let’s dig in to the basics of JAXP then.

JAXP, it stands for Java API for XML Processing. The on-going API version for JAXP is 1.6.

Now, one of the most interesting query about JAXP is what sort of API actually it provides. I mean what is this all about.

Simply if we say, it is an interface or a kind of plug-in provided or a layer provided to plug in various implementations whether it is own or a third party vendor implementation for XML processing. So what kind of XML processing it will do, The Plug-in API provides you platform for processing an XML in terms of Parsing, Presentation, and Streaming etc.

Well! Again term comes here, Right!! Parsing. We have already seen SAX and DOM parser in previous blogs. What we are going to discuss here about parsing is the basics architecture and little more examples with in-depth explanation for SAX and DOM API provided inside JAXP API. Other than that we will also see the basics of some other XML processing task and How JAXP will help to achieve them.

There are various packages define with in JAXP API for these different tasks are:-

  • javax.xml.parser: This package provides a common interface for SAX and DOM parsing activities.
  • org.w3c.dom: This package having components that defines XML as a Document class and also some more component classes are available for other elements of XML file like Attributes, Node etc.
  • org.xml.sax: This package provides basic API for SAX.
  • javax.xml.transform: This package contains API for transforming XML in to various other forms in terms of presentation. These transformations API are called XSLT API. We will see later what this is exactly.
  • javax.xml.stream: This is the very latest API included in JAXP from version JAXP 1.4 i.e streaming API for XML also in short term it is called as stAX. Remember ‘st’ is in small letter and ‘AX’ in caps letter. We will later discuss about this as well.

We have discuss some part of parsing stuff with SAX and DOM in my previous post. Where we have seen the Basics of XML as well and also how to parse them using SAX and DOM parser. Now we will see more basics about its architecture and some more examples in terms of JAXP API. We will see all the above JAXP packages provided API in detail in our next blog sections on:

SAX API  
DOM API
XSLT API
stAX API