Java API for XML Processing XML parsing model
- Slides: 12
Java API for XML Processing XML parsing model SAX
Contents n n What is JAXP ? Uses for JAXP API Model & Implementations Processing XML with SAX
What is JAXP ? n Java API for XML Parsing n JAXP 1. 0 (1999) n n JAXP 1. 1 n n Supported SAX 1. 0 and DOM Level 1 Supports SAX 2. 0 & extension, DOM Level 2 JAXP 1. 3 ( included in J 2 SE 5. 0 ) n n Supports SAX 2. 0. 2, DOM Level 3 Include Xerces ver 2. 6. 2+
Uses for JAXP n Pluggability layer n n n 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 n Parsing classes n n Transformaction classes n n Javax. xml. parsers package Javax. xml. transform package Many implementation n n Sun Crimson(included JDK 1. 4) Apache. Xerces, IBM, Oracle
Processing XML with SAX (1) n Event-driven processing model n n Data elements are interpreted and callback Biggest advantage n Not load XML documents ( fast, lightweight )
Processing XML with SAX (2) n Steps n 1. Getting Factory and Parser class n 2. Setting options n Setting namespace, validation, features n 3. Implement Default. Handler class n 4. Using SAX Parser
Processing XML with SAX - Getting Factory and Parser class try { SAXParser. Factory sax. Factory = SAXParser. Factory. new. Instance(); SAXParser sax. Parser = sax. Factory. new. SAXParser(); } catch ( Factory. Configuration. Error fce ) { // handle exception here } catch ( Parser. Configuration. Error pce ) { // handle exception here }
Processing XML with SAX - Setting options Setting Namespace factory. set. Name. Space. Aware(boolean) boolean parser. is. Namespace. Aware() Setting Validation factory. set. Validation(boolean) boolean parser. is. Validating() Setting Features factory. set. Feature(str. Name, boolean) boolean factory. get. Feature(str. Name)
Processing XML with SAX - Implement Default. Handler class public class My. SAXHandler extends Default. Handler { public My. SAXHandler() { } // overiding method public void start. Document() throws SAXException { } public void end. Document() throws SAXException { } public void characters(char[] buf, int offset, int len) throws SAXException { } public void start. Element(String namespace. URI, String local. Name, String row. Name, Attributes attrs ) throws SAXException { } public void end. Element(String namespace. URI, String local. Name, String row. Name) throws SAXException { }
Processing XML with SAX - Using SAX Parser
Sample SAX Parsing