Java API for XML Processing (JAXP) - Interview Questions

What is the JAXP API?


JAXP - Java API for XML processing - is an API provided in Java SE that is used for parsing XML documents using Java programming language. JAXP supports XML parser standards - Simple API for XML Parsing (SAX), Document Object Model (DOM) and Streaming API for XML (StAX).

JAXP also supports 'Extensible Stylesheet Language Transformation (XSTL)' that can be used to convert the XML data to other XML documents as well as other formats such as HTML.

What is SAX parser? How is it supported in the JAXP API


Simple API for XML (SAX) parser processes the XML document element-by-element. SAX parser is event driven and uses serial-access mechanism to process the XML document.

javax.xml.parsers package in Java SE provides the SAX specific APIs.

What is DOM parser? How is it supported in the JAXP API


Document Object Model (DOM) parser processes an XML document into a tree structure of objects and stores the entire object model to memory. Once the object model is stored in memory, it can be traversed, accessed and manipulated by a Java program.

javax.xml.parsers package in Java SE provides the DOM specific APIs.

What is StAX parser? How is it supported in the JAXP API


Streaming API for XML (StAX) parser processes an XML document element-by-element and is based on Java streaming. package in Java SE provides the StAX specific APIs.

StAX has a simpler programming model compaed to SAX, and is more memory efficient compared to DOM.

What are the key classes and interfaces defined in the JAXP API that supports SAX parsers?


The JAXP API provides the following classes and interfaces for processing XML documents using a SAX parser.

SAXParserFactory - SAXParserFactory creates an instance of SAXParser.

SAXParser - SAXParser interface defines overloaded parse() methods. Generally you pass the source XML document and an handler object that implements the interface DefaultHandler to the parse() method.

SAXReader - SAXReader is wrapped by the SAXParser. SAXReader is used for readinmg the XML document and communicating with the SAX event handlers.

ContentHandler - ContentHandler interface defines methods such as startDocument, endDocument, startElement, endElement which are invoked when an XML element is processed.

ErrorHandler - ErrorHandler interface defines the methods error(), fatalError() and warning() which are invoked when the parser encounters error while parsing the XML document.

DTDHandler - DTDHandler defines methods that are used for processing a DTD document.

DefaultHandler - DefaultHandler class implements the ContentHandler, ErrorHandler, DTDHandler, and EntityResolver interfaces. You generally extend DefaultHandler and override only the appropriate methods.

Subscribe to our Questions


Java EE - Interview Questions

Java ServletsJavaServer PagesJavaServer FacesJava API for XML Processing (JAXP)Java API for JSON Processing (JSON-P)Java Message Service API (JMS)Java Persistance API (JPA)Java Transaction API (JTA)JAX-WSJava API for RESTful Web Services (JAX-RS)Java Database Connectivity API (JDBC)Security
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example