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) 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 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 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
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 ) { }
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)
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)
Processing XML with SAX - Case 1 SAXFactory - DefaultHandler Implement ( Adapter Class ) ContentHandler ErrorHandler DTDHandler EntityResolver
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 { } }
Processing XML with SAX - Case 1 - DefaultHandler class 2 Parameter String namespaceURI http://ggaman.com String localname nick String qName a:nick Attributes attrs Info=“조찬제” <?xml version=“1.0”?> <GGAMAN xmlns:g="http://ggaman.com"> <info name=“조찬제”> <g:nick>Chan</g:nick> </info> </GGAMAN>
Processing XML with SAX - Case 1 - Using SAX Parser sp.parse("simplerss.xml",myHandler);
Processing XML with SAX Case 2 - XMLReader Steps 1. Getting XMLReader Class 2. Set ContentHandler ( and other ) 3. Using parser Method
Processing XML with SAX Case 2 - XMLReader XMLReader xmlReader = XMLReaderFactory.createXMLReader(); xmlReader.setContentHandler(myHandler); // xmlReader.setDTDHandler(someHandler); xmlReader.parse("simplerss.xml");
Example