Welcome to the XML Tutorial on SAX Parser! Let's dive into the world of SAX Parser and understand how it helps in parsing XML documents.
š” SAX (Simple API for XML) Parser is a streaming event-based API for XML processing. Unlike DOM (Document Object Model), SAX does not build an entire tree structure in memory. Instead, it calls user-defined callback functions as it encounters XML elements.
ā Memory Efficiency: SAX Parser is useful when dealing with very large XML documents. As it processes the XML document event by event, it uses less memory compared to DOM Parser that loads the entire XML document into memory.
š The SAX API consists of five main interfaces:
ContentHandler: Defines methods to handle content events like startElement, endElement, and characters.EntityResolver: Defines methods to resolve external entities.ErrorHandler: Defines methods to handle errors and warnings.DTDHandler: Defines methods to handle DTD (Document Type Definition) events.XMLReader: Acts as a factory for creating SAX XMLReader objects.šÆ Let's write a simple SAX Parser that prints the title and author of an XML document.
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SAXParserExample {
public static void main(String[] args) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
MyHandler handler = new MyHandler();
xmlReader.setContentHandler(handler);
xmlReader.parse(new java.io.File("book.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
}What is the purpose of the `MyHandler` class in the given code?
MyHandler classš Let's define a MyHandler class that extends DefaultHandler and overrides the necessary methods to process our XML document.
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public class MyHandler extends DefaultHandler {
private String title = null;
private String author = null;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("book")) {
// Initialize title and author variables
title = null;
author = null;
}
if (qName.equalsIgnoreCase("title")) {
title = null;
}
if (qName.equalsIgnoreCase("author")) {
author = null;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String content = new String(ch, start, length).trim();
if (title == null && !content.isEmpty()) {
title = content;
}
if (author == null && !content.isEmpty()) {
author = content;
}
}
public void printBookDetails() {
if (title != null && author != null) {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
}
}
}šÆ With the book.xml file containing:
<book>
<title>Learning XML with SAX</title>
<author>John Doe</author>
</book>The output of the given code will be:
Title: Learning XML with SAX
Author: John Doe
š” SAX Parser is a powerful and memory-efficient tool for parsing XML documents. It's especially useful when dealing with large XML files or when quick processing is required. Now that you've understood the basics of SAX Parser, you can explore more advanced topics and applications.
Happy Coding! šÆ