Java SAX Parser: A Comprehensive Guide with Examples - Quiz
Total: 12 questions
1. Describe how SAX parser works.
Describe how SAX parser works.
SAX is based on an event model. The SAX parser reads an XML document from the beginning and fires off events when it founds a new element, attribute, piece of text or other component.
2. The advantages of SAX parsers.
The advantages of SAX parsers.
They are usually very fast.
3. How to tell JAXP which SAX2 implementation you're using in your application?
How to tell JAXP which SAX2 implementation you're using in your application?
Set the org.xml.sax.driver system property.
4. The primary listener interface in SAX.
The primary listener interface in SAX.
ContentHandler.
5. Which adapter class, which implements ContentHandler, does SAX provide?
Which adapter class, which implements ContentHandler, does SAX provide?
DefaultHandler.
6. Example of implementing ContentHandler, which prints out the names of all the elements in a parsed document.
Example of implementing ContentHandler, which prints out the names of all the elements in a parsed document.
public class SimpleHandler extends DefaultHandler {
public void startElement(String namespaceURI, String localName, String qName, Attributes atts){
System.out.println(localName);
}
}
7. Example of using an implementation of ContentHandler.
Example of using an implementation of ContentHandler.
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader parser = saxParser.getXMLReader();
parser.setContentHandler(new MyHandler());
8. Which interface is used to access attributes in SAX?
Which interface is used to access attributes in SAX?
Attributes.
9. Which SAX interfaces are used when parsing documents that are associated with a DTD?
Which SAX interfaces are used when parsing documents that are associated with a DTD?
the org.xml.sax.DTDHandler and org.xml.sax.EntityResolver.
10. Which SAX interface is used to notify the application about an error while parsing the XML document?
Which SAX interface is used to notify the application about an error while parsing the XML document?
org.xml.sax.ErrorHandler.
11. Will parser throw exceptions if an error occurres while parsing the XML document?
Will parser throw exceptions if an error occurres while parsing the XML document?
No, it will call the ErrorHandler methods.
12. Methods of ErrorHandler interface.
Methods of ErrorHandler interface.
void error(SAXParseException exception) throws SAXException;
void fatalError(SAXParseException exception) throws SAXException;
void warning(SAXParseException exception) throws SAXException;