Download presentation
Presentation is loading. Please wait.
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 Case 1 - SAXFactory
Steps 1. Getting Factory and Parser class 2. Setting options Setting namespace, validation, features 3. Implement DefaultHandler class 4. Using parser Method
8
Processing XML with SAX - Case 1 - 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 - Case 1 - 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 - Case 1 - 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)
11
Processing XML with SAX - Case 1 SAXFactory - DefaultHandler
Implement ( Adapter Class ) ContentHandler ErrorHandler DTDHandler EntityResolver
12
Processing XML with SAX - Case 1 - DefaultHandler class
public class MySAXHandler extends DefaultHandler { public MySAXHandler() { } // overiding method - contentHandler 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 { } }
13
Processing XML with SAX - Case 1 - DefaultHandler class 2
Parameter String namespaceURI String localname nick String qName a:nick Attributes attrs Info=“조찬제” <?xml version=“1.0”?> <GGAMAN xmlns:g=" <info name=“조찬제”> <g:nick>Chan</g:nick> </info> </GGAMAN>
14
Processing XML with SAX - Case 1 - Using SAX Parser
sp.parse("simplerss.xml",myHandler);
15
Processing XML with SAX Case 2 - XMLReader
Steps 1. Getting XMLReader Class 2. Set ContentHandler ( and other ) 3. Using parser Method
16
Processing XML with SAX Case 2 - XMLReader
XMLReader xmlReader = XMLReaderFactory.createXMLReader(); xmlReader.setContentHandler(myHandler); // xmlReader.setDTDHandler(someHandler); xmlReader.parse("simplerss.xml");
17
Example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.