Download presentation
1
Java API for XML Processing
XML parsing model SAX
2
Contents What is JAXP ? Uses for JAXP JAXP API Model & Implementations
Processing XML with SAX
3
What is JAXP ? Java API for XML Parsing JAXP 1.0 (1999) JAXP 1.1
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
4
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
5
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
6
Processing XML with SAX (1)
Event-driven processing model Data elements are interpreted and callback Biggest advantage Not load XML documents ( fast, lightweight )
7
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
8
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 ) { }
9
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)
10
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 { }
11
Processing XML with SAX - Using SAX Parser
12
Sample SAX Parsing
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.