Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped.

Similar presentations


Presentation on theme: "1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped."— Presentation transcript:

1 1 Introduction SAX

2 Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped Entities  Workshops

3 Working of SAX 3 1.An instance of SAXParserFactory is generated by the parser to initial the working of SAX 2.The parser encloses a SAXReader object 3.The parse() method of the SAXParser class is invoked 4.The SAXReader invokes a callback method

4 Example public class SAXProcessing extends DefaultHandler{ static int count = 0; public SAXProcessing () {} public static void main (String[] args) { … SAXProcessing saxObject = new SAXProcessing(); SAXParserFactory spf = SAXParserFactory.newInstance (); try { SAXParser parser = spf.newSAXParser (); parser.parse (new File(args[0]), saxObject); }catch(Exception e){ e.printStackTrace (); System.exit (1);} } public void startElement (String uri, String simpleName, String qualifiedName, Attributes attributeList) throws SAXException{ if(qualifiedName.equals ("user")){count++;} } public void endDocument () throws SAXException { System.out.println(count + " users defined on Tomcat Server");}

5 Example (cont)

6 Merit – DeMerit of SAX 6

7 Packages in JAXP for SAX PackageDescriptions org.xml.sax- Specifies the SAX interfaces org.xml.sax.ext - Defines the SAX extensions, which are used for extended SAX processing - Provides extension classes and interfaces for handling DTD declaration events and providing lexical information on the document being parsed org.xml.sax.helpers - Contains helper classes that make the SAX parsing easier. org.xml.sax.parsers- Lets the processing of XML documents

8 JAXP API InterfacesDescriptions XMLReader - public abstract interface XMLReader - Reads XML document, which uses callback methods -. The different callback methods which helps the XMLReader interface are getContentHandler(), getHandler(), getEntityResolver() and getErrorHandler() XMLFilter - public abstract interface XMLFilter extends XMLReader - Filters XML document. - The used methods are setParent() and getParent() DeclHandler - public abstract interface DeclHandler - Is an optional extension handler, which provides information about Document Type Declarations in an XML document - The methods used by DecHandler interface are attributeDecl(), elementDecl(), externalEntityDecl(), and internalEntityDecl() LexicalHandler - public abstract interface LexicalHandler - Is an optional extension handler, which provides lexical information about an XML document - The methods used by LexicalHsandler interface are comment(), endCDATA(), and endDTD()

9 JAXP API (cont) ClassesDescriptions SAXParser - public abstract class SAXParser extends java.lang.Object - Defines the API that encloses an XMLReader implementation. SAXParserFactory - public abstract class SAXParserFactory extends java.lang.Object - Defines a factory API that enables applications for configuring and obtaining a SAX based parser for parsing an XML document - The methods used by SAXParserFactory class are is NamespaceAware(), is Validating(), newInstance(), and newSAXParser XMLReaderFactory - public final class XMLReaderFactory extends Object - Is a factory for creating an XML reader - The methods used by XMLReaderFactory class are createXMLReader() and createXMLReader(String) SAXException - public class SAXException extends Exception - SAX error or warning. A SAX handler throws this exception whenever an error occurs during processing - The methods used by SAXException class are getException(), getMessage(), and toString().

10 Parsing XML Document 10

11 ContentHandler - DefaultHandler 11

12 Callback Interface //file Book.xml Harry Porter 35.0.... startDocument() startElement(“library”, attr) startElement(“book”, attr) startElement(“title”, attr) characters(char[], start, length) endDocument()... endElement(“library”)

13 Content Parsing API 13 public class Notification extends DefaultHandler { public void startDocument() throws SAXException { ……} public void endDocument() throws SAXException { ……} public void startElement(String uri, String name, String qname, Attributes attr)throws SAXException { …….} public void characters(char[] ch, int start, int length) throws SAXException { ……..} public void endElement(String uri, String name, String qname)throws SAXException { ………} }

14 Parsing …. 14 public static void main (String[] args) { try { XMLReader parser = XMLReaderFactory.createXMLReader(); Notification notify=new Notification(); parser.setContentHandler(notify); parser.parse(args[0]); } catch(Exception e) { e.printStackTrace(); } Harry Porter URI local

15 SAXParserFactory 15 MethodsDescriptions newInstance - public static SAXParserFactory newInstance() throws FactoryConfigurationError - Is a static method, which creates a new instance of Factory by receiving it from SAXParserFactory - Ex: SAXParserFactory spf = SAXParserFactory.newInstance(); newSAXParser - public abstract SAXParser newSAXParser() throws ParserConfigurationException, SAXException - Creates a new instance of a SAXParser with configured Factory parameters - Ex: SAXParser parser = spf.newSAXParser(); setNamespaceAware - public void setNamespaceAware(boolean awareness) - Specifies that the code produced by the parser supports XML namespaces setValidating - public void setValidating(boolean validating) - Specifies that the parser produced by this code validates whenever they are parsed. Its default value is set to false isNamespaceAware - public boolean isNamespaceAware() - Gives a confirmation about the Factory class for producing the parser that is aware of namespace isValidating - public boolean isValidating() - Confirms the configuration of a Factory class for validating the XML content during parsing

16 Demo … 16 Object of class extends DefaultHandler

17 Methods in the Attribute Interface 17 MethodsDescriptions getLength - public int getLength() - Returns the number of attributes present in a list getURI - public String getURI(int index) - Refers to an attribute’s Namespace URI by an index getLocalName - public String getLocalName(int index) - Refers to an attribute’s local name by an index getQName - public String getQName(int index) - Refers to an attribute’s XML 1.0 qualified name by an index. getType - public String getType(int index) - Refers to an attribute’s type by an index getValue - public String getValue(int index) - Refers to an attribute’s value by an index getIndex - public int getIndex(String uri, String localPart) - Refers to the index of an attribute by a Namespace name

18 “Locator” interface 18 MethodsDescriptions getPublicId - public String getPublicId() - Retrieves the public identifier for the current document event. getSystemId - public String getSystemId - Retrieves the system identifier for the current document event. getLineNumber - public int getLineNumber() - Retrieves the line number at the end of the current document event getColumnNumber - public int getColumnNumber() - Retrieves the column number at the end of the current document event. A document location is found by the use of Locator interface. A Locator object carries the information necessary to find the location of a document. The Locator class encapsulates a system ID or a public identifier or both. A default location is set by the setDocumentLocator before the beginning or parsing and the location of the document is found

19 Example public class SAXLocator extends DefaultHandler { private Locator locator; public static void main (String[] args) { SAXParserFactory spf = SAXParserFactory.newInstance (); spf.setValidating (true); try { SAXParser parser = spf.newSAXParser (); parser.parse (new File(args[0]), new SAXLocator()); } catch (SAXException ex) {ex.printStackTrace(); } catch (ParserConfigurationException ex) {ex.printStackTrace(); } catch (IOException e){e.printStackTrace ();}} public void setDocumentLocator (Locator locator) { this.locator = locator;} public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException { if(qName.equals ("user")){ System.out.println("process element start"); } else { String location=""; if(locator!=null){ location = locator.getSystemId ();//XML document name location+= " line " + locator.getLineNumber () + " column " + locator.getColumnNumber () + ";"; } System.out.println("location: " + location);}}}

20 ProcessingInstruction 20 public class SAXInstructionsProcessing extends DefaultHandler { public SAXInstructionsProcessing () {} public static void main (String[] args) { try { SAXInstructionsProcessing saxObject = new SAXInstructionsProcessing(); SAXParserFactory spf = SAXParserFactory.newInstance (); SAXParser parser = spf.newSAXParser (); parser.parse (new File(args[0]), saxObject); }catch(Exception e){ e.printStackTrace (); System.exit (1);} } public void processingInstruction (String target, String data) throws SAXException { System.out.println("target: " + target); System.out.println ("data: " + data); }

21 Entities & Skipped Entities  An Entity is an XML file refers to a certain piece of data or text or an entire document. SAX parser handlers these entities by the use of Entity interface.  An entity declared in an external DTD is known as skipped entity by SAX2. The value of this non- declared is empty by default.  Entities in attributes are not reported rather they are skipped

22 WORKSHOP ACTIVITIES Building the console Java application using SAX parser can do as Count the element in XML file Processing the XML body to print all element in XML file Print the attributes in element in XML file


Download ppt "1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped."

Similar presentations


Ads by Google