Understanding JAXP and SAX for XML Processing in Java
This guide provides an overview of the Java API for XML Processing (JAXP) and its use with Simple API for XML (SAX) for efficient XML parsing. It covers the key features of JAXP, including its support for SAX and DOM models, the ability to change XML parsers without altering application logic, and its implementation details. You will learn how to set up the SAX parser, configure options like namespaces and validation, and implement the DefaultHandler class for custom event handling during XML parsing.
Understanding JAXP and SAX for XML Processing in Java
E N D
Presentation Transcript
Java API for XML Processing XML parsing model SAX
Contents • What is JAXP ? • Uses for JAXP • JAXP API Model & Implementations • Processing XML with SAX
What is JAXP ? • Java API for XML Parsing • JAXP 1.0 (1999) • Supported SAX 1.0 and DOM Level 1 • JAXP 1.1 • Supports SAX 2.0 & extension, DOM Level 2 • JAXP 1.3 ( included in J2SE 5.0 ) • Supports SAX 2.0.2, DOM Level 3 • Include Xerces ver 2.6.2+
Uses for JAXP • Pluggability layer • Change parser without affecting the application logic • Standard interfaces that encapsulate the details behind parser interactions • choose a parser provider that best suits the requirements of the service
JAXP API Model & Implementations • Parsing classes • Javax.xml.parsers package • Transformaction classes • Javax.xml.transform package • Many implementation • Sun Crimson(included JDK1.4) • ApacheXerces, IBM, Oracle
Processing XML with SAX (1) • Event-driven processing model • Data elements are interpreted and callback • Biggest advantage • Not load XML documents ( fast, lightweight )
Processing XML with SAX (2) • Steps • 1. Getting Factory and Parser class • 2. Setting options • Setting namespace, validation, features • 3. Implement DefaultHandler class • 4. Using SAX Parser
Processing XML with SAX- Getting Factory and Parser class try { SAXParserFactory saxFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxFactory.newSAXParser(); } catch ( FactoryConfigurationError fce ) { // handle exception here } catch ( ParserConfigurationError pce ) { // handle exception here }
Processing XML with SAX- Setting options Setting Namespace factory.setNameSpaceAware(boolean) boolean parser.isNamespaceAware() Setting Validation factory.setValidation(boolean) boolean parser.isValidating() Setting Features factory.setFeature(strName, boolean) boolean factory.getFeature(strName)
Processing XML with SAX- Implement DefaultHandler class public class MySAXHandler extends DefaultHandler { public MySAXHandler() { } // overiding method public void startDocument() throws SAXException { } public void endDocument() throws SAXException { } public void characters(char[] buf, int offset, int len) throws SAXException { } public void startElement(String namespaceURI, String localName, String rowName, Attributes attrs ) throws SAXException { } public void endElement(String namespaceURI, String localName, String rowName) throws SAXException { }