What is XML parser? XML parser is a software library (or package) that reads and parses XML. It is designed to read XML and create a way for programs. Different types of XML persers are available for various languages like: C++, Java, C#, Perl, Python, PHP, Ruby etc.
Types of XML parsers in Java
JAXP JAXP stands for Java API for XML Processing. It is available as a seperate library or as a part of Sun JDK 1.4 (javax.xml.parsers package). It is an independent way of writing java code for XML. It supports the SAX and DOM parser API's and the XSLT standard. Also it allows to plug in implementation of the parser or the processor. By default it uses reference implementations(crimson and xalan)
SAX: Simple API for XMLIt is an event driven processing of XML documents, it implements SAX API. it reads each unit of XML, it creates an event which can be used by calling program. It is used for high performance applications or areas where XML size might exceed the available momory. SAX reads the XML documents as a stream of XML tags(starting elements, ending elements, text sections etc). Programmer decides what to do with every event. SAX parser does not create any object at all, it simply delivers events. It provides interfaces on handlers. The design specification and co-ordination was done by Dave Megginson. Current SAX stadard is version 2.0.
There are four handler interfaces: ContentHandler interface, DTDHandler interface, EntityResolver interface and ErrorHandler interface.
DOM: Document Object Model DOM parser is a tree based API, it implements DOM API. It is different from SAX perser because it builds the entire XML representaion in memory and used a as a whole. It can be very memory related. Current DOM stadard is version level 3.0. It provides interfaces on components of a tree such as: Document interface, Node interface, NodeList interface, Element interface, Attr interface etc. With DOM parcer, method calls in client application have to be explicit and forms like a chained method calls.
StAX: Streaming API for XML StAX pull perser has been implemented in Java by supporting JSP-173. This parser pulls the required data from XML. It can give a significance performance improve. It maintains a cursor at the current position.
Difference between SAX parser and StAX parser:
Can DOM and SAX parsers be used at the same time? Yes we can, because the use of a DOM and a SAX parser is independent.
Difference between DOM and SAX parser
Conclusion The choice among SAX, DOM and StAX for any given application is the matter of testing. It checks whether the document well formatted and validated or not. It also parses data to the invoking application.