SlideShare a Scribd company logo
SAX PARSER
Introduction
 An event-based parser for xml documents.
 SAX parser creates no parse tree
 SAX is a streaming interface for XML, which means
that applications using SAX receive event notifications
about the XML document being processed an
element, and attribute, at a time in sequential order
starting at the top of the document, and ending with
the closing of the ROOT element.
When to use?
SAX parser is used when:
 The XML document is processes in a linear fashion
from the top down
 The document is not deeply nested
 Very large XML document is processed whose
DOM tree would consume too much memory.
 The problem to be solved involves only part of the
XML document
SAX vs DOM
S.N
o
SAX DOM
1 Simpler More complex than SAX
2 Event based parser Tree based parser
3 Smaller than DOM Larger
4 SAX parses the file at it reads i.e.
Parses node by node.
DOM loads the file into the memory
and then parse the file.
5 Use SAX parser when memory content
is large.
If the XML content is small then
prefer DOM parser.
6 Slower than DOM Faster than SAX
7 Backward and forward search is
possible for searching the tags and
evaluation of the information inside the
tags. So this gives the ease of
navigation.
SAX reads the XML file from top to
bottom and backward navigation is
not possible.
Disadvantages of SAX
 We have no random access to an XML
document since it is processed in a forward-
only manner
 SAX is read only parser.
SAX parser
 Provides framework for defining event
listeners or handlers
 Handlers are registered with SAX to receive
events
 Events include
 Start of document
 Start of element (element’s starting tag)
 End of element (element’s ending tag)
 Character data
 End of document
Example
<?xml
version=“1.0”?>
<fiction>
<book>XYZ</book>
</fiction>
Events
--------
Start document
Start element : fiction
Start element: book
Characters: XYZ
End element: book
End element: fiction
End of document
SAX packages
 SAX 2.0 API
 2 standard packages
 org.xml.sax - contains basic classes,
interfaces, exceptions needed for parsing
documents.
 org.xml.helpers -
 1 extension package
 org.xml.sax.ext – used for capturing
declaration and lexical events
Org.xml.sax package
 Interfaces
AttributeList, Attributes, ContentHandler,
DocumentHandler,DTDHandler,EntityHandler,
ErrorHandler, Locator, Parser,
XMLFilter,XMLReader
 Classes
HandlerBase, InputSource
 Exceptions
SAXException, SAXNotRecognizedException,
SAXNotSupportedException,
SAXParseException
Org.xml.sax.helpers package
 Classes
AttributeListTmpl, AttributeImpl,
DefaultHandler, LocatorImpl
NamespaceSupport, ParserAdapter,
ParserFactory, XMLFilterImpl,
XMLReaderAdapter, XMLREaderFactory
 Org.xml.sax.ext package
 Interfaces
 DeclHandler, LexicalHandler
Simple SAX program
 The program consists of two classes:
 Sample -- This class contains the main method; it
 Gets a factory to make parsers
 Gets a parser from the factory
 Creates a Handler object to handle callbacks from the parser
 Tells the parser which handler to send its callbacks to
 Reads and parses the input XML file
 Handler -- This class contains handlers for three kinds of
callbacks:
 startElement callbacks, generated when a start tag is seen
 endElement callbacks, generated when an end tag is seen
 characters callbacks, generated for the contents of an element
Example
import javax.xml.parsers.*; // for both SAX and DOM
import org.xml.sax.*;
import org.xml.sax.helpers.*;
// For simplicity, we let the operating system handle exceptions
// In "real life" this is poor programming practice
public class Sample {
public static void main(String args[]) throws Exception {
// Create a parser factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// Tell factory that the parser must understand namespaces
factory.setNamespaceAware(true);
// Make the parser
SAXParser saxParser = factory.newSAXParser();
XMLReader parser = saxParser.getXMLReader();
Example
 In the previous slide we made a parser, of type
XMLReader
 Now we continue with:
// Create a handler
Handler handler = new Handler();
// Tell the parser to use this handler
parser.setContentHandler(handler);
// Finally, read and parse the document
parser.parse("hello.xml");
} // end of Sample class
 You will need to put the file hello.xml :
 In the same directory, if you run the program from the command
line
Example
 public class Handler extends DefaultHandler {
 DefaultHandler is an adapter class that defines these methods
and others as do-nothing methods, to be overridden as desired
 We will define three very similar methods to handle (1) start tags,
(2) contents, and (3) end tags--our methods will just print a line
 Each of these three methods could throw a SAXException
 // SAX calls this method when it encounters a start tag
public void startElement(String namespaceURI,
String localName,
String qualifiedName,
Attributes attributes)
throws SAXException {
System.out.println("startElement: " + qualifiedName);
}
Example
 // SAX calls this method to pass in character data
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println("characters: " +
new String(ch, start, length));
}
 // SAX call this method when it encounters an end tag
public void endElement(String namespaceURI,
String localName,
String qualifiedName)
throws SAXException {
System.out.println("Element: /" + qualifiedName);
}
} // End of Handler class
Results
 If the file hello.xml contains:
<?xml version="1.0"?>
<display>Hello World!</display>
 Then the output from running java Sample will
be:
startElement: display
characters: Hello World!
Element: /display
Parser factories
 A factory is an alternative to constructors
 To create a SAX parser factory, call this method:
SAXParserFactory.newInstance()
 This returns an object of type SAXParserFactory
 It may throw a FactoryConfigurationError
 You can then say what kind of parser you want:
 public void setNamespaceAware(boolean awareness)
 Call this with true if you are using namespaces
 The default (if you don’t call this method) is false
 public void setValidating(boolean validating)
 Call this with true if you want to validate against a DTD
 The default (if you don’t call this method) is false
 Validation will give an error if you don’t have a DTD
Getting a parser
 Once you have a SAXParserFactory set up (say it’s
named factory), you can create a parser with:
SAXParser saxParser = factory.newSAXParser();
XMLReader parser = saxParser.getXMLReader();
 Note: older texts may use Parser in place of XMLReader
 Parser is SAX1, not SAX2, and is now deprecated
 SAX2 supports namespaces and some new parser properties
Declaring which handler to use
 Since the SAX parser will be calling our methods, we
need to supply these methods
 In the example these are in a separate class, Handler
 We need to tell the parser where to find the methods:
Handler handler = new Handler();
parser.setContentHandler(handler);
 These statements could be combined:
parser.setContentHandler(new Handler());
 Finally, we call the parser and tell it what file to parse:
parser.parse("hello.xml");
 Everything else will be done in the handler methods
SAX handlers
 A callback handler for SAX must implement these
four interfaces:
 interface ContentHandler
 This is the most important interface--it handles
basic parsing callbacks, such as element starts and
ends
 interface DTDHandler
 Handles only notation and unparsed entity
declarations
 interface EntityResolver
 Does customized handling for external entities
 interface ErrorHandler
 Must be implemented or parsing errors will be
ignored!
 You could implement all these interfaces yourself, but
that’s a lot of work--it’s easier to use an adapter class
Class DefaultHandler
 DefaultHandler is in package org.xml.sax.helpers
 DefaultHandler implements ContentHandler,
DTDHandler, EntityResolver, and ErrorHandler
 DefaultHandler is an adapter class--it provides empty
methods for every method declared in each of the four
interfaces
 Empty methods don’t do anything
 To use this class, extend it and override the methods that
are important to your application
 We will cover some of the methods in the ContentHandler and
ErrorHandler interfaces
ContentHandler Interface
This interface specifies the callback methods that
the SAX parser uses to notify an application program
of the components of the XML document that it has
seen.
void startDocument() - Called at the beginning of a
document.
void endDocument() - Called at the end of a
document.
void startElement(String uri, String localName,
String qName, Attributes atts) - Called at the
beginning of an element.
ContentHandler interface
 void characters(char[] ch, int start, int length) - Called when
character data is encountered.
 void ignorableWhitespace( char[] ch, int start, int length) -
Called when a DTD is present and ignorable whitespace is
encountered.
 void processingInstruction(String target, String data) -
Called when a processing instruction is recognized.
 void setDocumentLocator(Locator locator)) - Provides a
Locator that can be used to identify positions in the document.
 void skippedEntity(String name) - Called when an unresolved
entity is encountered.
 void startPrefixMapping(String prefix, String uri) - Called
when a new namespace mapping is defined.
 void endPrefixMapping(String prefix) - Called when a
namespace definition ends its scope.
Attributes Interface
This interface specifies methods for processing
the attributes connected to an element.
 int getLength() - Returns number of attributes.
 String getQName(int index)
 String getValue(int index)
 String getValue(String qname)
Parsing XML document
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class UserHandler extends DefaultHandler
{
boolean bFirstName = false;
boolean bLastName = false;
boolean bNickName = false;
boolean bMarks = false;
Parsing XML document
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if (qName.equalsIgnoreCase("student"))
{
String rollNo = attributes.getValue("rollno");
System.out.println("Roll No : " + rollNo);
}
else if (qName.equalsIgnoreCase("firstname")) { bFirstName = true;
}
else if (qName.equalsIgnoreCase("lastname")) { bLastName = true;
}
else if (qName.equalsIgnoreCase("nickname")) { bNickName = true;
}
else if (qName.equalsIgnoreCase("marks")) { bMarks = true; }
};
Parsing XML document
public void endElement(String uri, String
localName, String qName) throws
SAXException
{
if (qName.equalsIgnoreCase("student"))
{
System.out.println("End Element :" +
qName);
}
}
Parsing XML document
public void characters(char ch[], int start, int length) throws SAXException)
{ if (bFirstName)
{ System.out.println("First Name: " + new String(ch, start, length)); bFirstName =
false;
}
else if (bLastName)
{ System.out.println("Last Name: " + new String(ch, start, length)); bLastName =
false;
}
else if (bNickName)
{
System.out.println("Nick Name: " + new String(ch, start, length)); bNickName =
false;
}
else if (bMarks)
Parsing XML document
public class SAXParserDemo
{
public static void main(String[] args)
{
try
{
File inputFile = new File("input.txt");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
UserHandler userhandler = new UserHandler();
saxParser.parse(inputFile, userhandler);
}
catch (Exception e) { e.printStackTrace(); }
}
}

More Related Content

What's hot

Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
yht4ever
 
Client side scripting using Javascript
Client side scripting using JavascriptClient side scripting using Javascript
Client side scripting using Javascript
Bansari Shah
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQuery
Katrien Verbert
 

What's hot (20)

XSLT
XSLTXSLT
XSLT
 
Restful web services ppt
Restful web services pptRestful web services ppt
Restful web services ppt
 
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
 
Web Servers (ppt)
Web Servers (ppt)Web Servers (ppt)
Web Servers (ppt)
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Client side scripting using Javascript
Client side scripting using JavascriptClient side scripting using Javascript
Client side scripting using Javascript
 
HTTP request and response
HTTP request and responseHTTP request and response
HTTP request and response
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Xml presentation
Xml presentationXml presentation
Xml presentation
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Json
JsonJson
Json
 
Document Type Definition (DTD)
Document Type Definition (DTD)Document Type Definition (DTD)
Document Type Definition (DTD)
 
Get method and post method
Get method and post methodGet method and post method
Get method and post method
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Basic html structure
Basic html structureBasic html structure
Basic html structure
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQuery
 
XML
XMLXML
XML
 
XML Schema
XML SchemaXML Schema
XML Schema
 

Similar to Sax parser

Service Oriented Architecture - Unit II - Sax
Service Oriented Architecture - Unit II - Sax Service Oriented Architecture - Unit II - Sax
Service Oriented Architecture - Unit II - Sax
Roselin Mary S
 
InstructionYou’ll probably want to import FileReader, PrintWriter,.pdf
InstructionYou’ll probably want to import FileReader, PrintWriter,.pdfInstructionYou’ll probably want to import FileReader, PrintWriter,.pdf
InstructionYou’ll probably want to import FileReader, PrintWriter,.pdf
arsmobiles
 
JSR 172: XML Parsing in MIDP
JSR 172: XML Parsing in MIDPJSR 172: XML Parsing in MIDP
JSR 172: XML Parsing in MIDP
Jussi Pohjolainen
 

Similar to Sax parser (20)

SAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginnersSAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginners
 
XML SAX PARSING
XML SAX PARSING XML SAX PARSING
XML SAX PARSING
 
Python xml processing
Python   xml processingPython   xml processing
Python xml processing
 
5 xml parsing
5   xml parsing5   xml parsing
5 xml parsing
 
Java XML Parsing
Java XML ParsingJava XML Parsing
Java XML Parsing
 
Service Oriented Architecture - Unit II - Sax
Service Oriented Architecture - Unit II - Sax Service Oriented Architecture - Unit II - Sax
Service Oriented Architecture - Unit II - Sax
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
JAXP
JAXPJAXP
JAXP
 
Sax Dom Tutorial
Sax Dom TutorialSax Dom Tutorial
Sax Dom Tutorial
 
XML
XMLXML
XML
 
Ch23
Ch23Ch23
Ch23
 
Ch23 xml processing_with_java
Ch23 xml processing_with_javaCh23 xml processing_with_java
Ch23 xml processing_with_java
 
24sax
24sax24sax
24sax
 
Unit 2.3
Unit 2.3Unit 2.3
Unit 2.3
 
Stax parser
Stax parserStax parser
Stax parser
 
InstructionYou’ll probably want to import FileReader, PrintWriter,.pdf
InstructionYou’ll probably want to import FileReader, PrintWriter,.pdfInstructionYou’ll probably want to import FileReader, PrintWriter,.pdf
InstructionYou’ll probably want to import FileReader, PrintWriter,.pdf
 
Unit 2.3
Unit 2.3Unit 2.3
Unit 2.3
 
JSR 172: XML Parsing in MIDP
JSR 172: XML Parsing in MIDPJSR 172: XML Parsing in MIDP
JSR 172: XML Parsing in MIDP
 
Xml parsing
Xml parsingXml parsing
Xml parsing
 
Extensible markup language attacks
Extensible markup language attacksExtensible markup language attacks
Extensible markup language attacks
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 

Sax parser

  • 2. Introduction  An event-based parser for xml documents.  SAX parser creates no parse tree  SAX is a streaming interface for XML, which means that applications using SAX receive event notifications about the XML document being processed an element, and attribute, at a time in sequential order starting at the top of the document, and ending with the closing of the ROOT element.
  • 3. When to use? SAX parser is used when:  The XML document is processes in a linear fashion from the top down  The document is not deeply nested  Very large XML document is processed whose DOM tree would consume too much memory.  The problem to be solved involves only part of the XML document
  • 4. SAX vs DOM S.N o SAX DOM 1 Simpler More complex than SAX 2 Event based parser Tree based parser 3 Smaller than DOM Larger 4 SAX parses the file at it reads i.e. Parses node by node. DOM loads the file into the memory and then parse the file. 5 Use SAX parser when memory content is large. If the XML content is small then prefer DOM parser. 6 Slower than DOM Faster than SAX 7 Backward and forward search is possible for searching the tags and evaluation of the information inside the tags. So this gives the ease of navigation. SAX reads the XML file from top to bottom and backward navigation is not possible.
  • 5. Disadvantages of SAX  We have no random access to an XML document since it is processed in a forward- only manner  SAX is read only parser.
  • 6. SAX parser  Provides framework for defining event listeners or handlers  Handlers are registered with SAX to receive events  Events include  Start of document  Start of element (element’s starting tag)  End of element (element’s ending tag)  Character data  End of document
  • 7. Example <?xml version=“1.0”?> <fiction> <book>XYZ</book> </fiction> Events -------- Start document Start element : fiction Start element: book Characters: XYZ End element: book End element: fiction End of document
  • 8. SAX packages  SAX 2.0 API  2 standard packages  org.xml.sax - contains basic classes, interfaces, exceptions needed for parsing documents.  org.xml.helpers -  1 extension package  org.xml.sax.ext – used for capturing declaration and lexical events
  • 9. Org.xml.sax package  Interfaces AttributeList, Attributes, ContentHandler, DocumentHandler,DTDHandler,EntityHandler, ErrorHandler, Locator, Parser, XMLFilter,XMLReader  Classes HandlerBase, InputSource  Exceptions SAXException, SAXNotRecognizedException, SAXNotSupportedException, SAXParseException
  • 10. Org.xml.sax.helpers package  Classes AttributeListTmpl, AttributeImpl, DefaultHandler, LocatorImpl NamespaceSupport, ParserAdapter, ParserFactory, XMLFilterImpl, XMLReaderAdapter, XMLREaderFactory  Org.xml.sax.ext package  Interfaces  DeclHandler, LexicalHandler
  • 11. Simple SAX program  The program consists of two classes:  Sample -- This class contains the main method; it  Gets a factory to make parsers  Gets a parser from the factory  Creates a Handler object to handle callbacks from the parser  Tells the parser which handler to send its callbacks to  Reads and parses the input XML file  Handler -- This class contains handlers for three kinds of callbacks:  startElement callbacks, generated when a start tag is seen  endElement callbacks, generated when an end tag is seen  characters callbacks, generated for the contents of an element
  • 12. Example import javax.xml.parsers.*; // for both SAX and DOM import org.xml.sax.*; import org.xml.sax.helpers.*; // For simplicity, we let the operating system handle exceptions // In "real life" this is poor programming practice public class Sample { public static void main(String args[]) throws Exception { // Create a parser factory SAXParserFactory factory = SAXParserFactory.newInstance(); // Tell factory that the parser must understand namespaces factory.setNamespaceAware(true); // Make the parser SAXParser saxParser = factory.newSAXParser(); XMLReader parser = saxParser.getXMLReader();
  • 13. Example  In the previous slide we made a parser, of type XMLReader  Now we continue with: // Create a handler Handler handler = new Handler(); // Tell the parser to use this handler parser.setContentHandler(handler); // Finally, read and parse the document parser.parse("hello.xml"); } // end of Sample class  You will need to put the file hello.xml :  In the same directory, if you run the program from the command line
  • 14. Example  public class Handler extends DefaultHandler {  DefaultHandler is an adapter class that defines these methods and others as do-nothing methods, to be overridden as desired  We will define three very similar methods to handle (1) start tags, (2) contents, and (3) end tags--our methods will just print a line  Each of these three methods could throw a SAXException  // SAX calls this method when it encounters a start tag public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attributes) throws SAXException { System.out.println("startElement: " + qualifiedName); }
  • 15. Example  // SAX calls this method to pass in character data public void characters(char ch[], int start, int length) throws SAXException { System.out.println("characters: " + new String(ch, start, length)); }  // SAX call this method when it encounters an end tag public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException { System.out.println("Element: /" + qualifiedName); } } // End of Handler class
  • 16. Results  If the file hello.xml contains: <?xml version="1.0"?> <display>Hello World!</display>  Then the output from running java Sample will be: startElement: display characters: Hello World! Element: /display
  • 17. Parser factories  A factory is an alternative to constructors  To create a SAX parser factory, call this method: SAXParserFactory.newInstance()  This returns an object of type SAXParserFactory  It may throw a FactoryConfigurationError  You can then say what kind of parser you want:  public void setNamespaceAware(boolean awareness)  Call this with true if you are using namespaces  The default (if you don’t call this method) is false  public void setValidating(boolean validating)  Call this with true if you want to validate against a DTD  The default (if you don’t call this method) is false  Validation will give an error if you don’t have a DTD
  • 18. Getting a parser  Once you have a SAXParserFactory set up (say it’s named factory), you can create a parser with: SAXParser saxParser = factory.newSAXParser(); XMLReader parser = saxParser.getXMLReader();  Note: older texts may use Parser in place of XMLReader  Parser is SAX1, not SAX2, and is now deprecated  SAX2 supports namespaces and some new parser properties
  • 19. Declaring which handler to use  Since the SAX parser will be calling our methods, we need to supply these methods  In the example these are in a separate class, Handler  We need to tell the parser where to find the methods: Handler handler = new Handler(); parser.setContentHandler(handler);  These statements could be combined: parser.setContentHandler(new Handler());  Finally, we call the parser and tell it what file to parse: parser.parse("hello.xml");  Everything else will be done in the handler methods
  • 20. SAX handlers  A callback handler for SAX must implement these four interfaces:  interface ContentHandler  This is the most important interface--it handles basic parsing callbacks, such as element starts and ends  interface DTDHandler  Handles only notation and unparsed entity declarations  interface EntityResolver  Does customized handling for external entities  interface ErrorHandler  Must be implemented or parsing errors will be ignored!  You could implement all these interfaces yourself, but that’s a lot of work--it’s easier to use an adapter class
  • 21. Class DefaultHandler  DefaultHandler is in package org.xml.sax.helpers  DefaultHandler implements ContentHandler, DTDHandler, EntityResolver, and ErrorHandler  DefaultHandler is an adapter class--it provides empty methods for every method declared in each of the four interfaces  Empty methods don’t do anything  To use this class, extend it and override the methods that are important to your application  We will cover some of the methods in the ContentHandler and ErrorHandler interfaces
  • 22. ContentHandler Interface This interface specifies the callback methods that the SAX parser uses to notify an application program of the components of the XML document that it has seen. void startDocument() - Called at the beginning of a document. void endDocument() - Called at the end of a document. void startElement(String uri, String localName, String qName, Attributes atts) - Called at the beginning of an element.
  • 23. ContentHandler interface  void characters(char[] ch, int start, int length) - Called when character data is encountered.  void ignorableWhitespace( char[] ch, int start, int length) - Called when a DTD is present and ignorable whitespace is encountered.  void processingInstruction(String target, String data) - Called when a processing instruction is recognized.  void setDocumentLocator(Locator locator)) - Provides a Locator that can be used to identify positions in the document.  void skippedEntity(String name) - Called when an unresolved entity is encountered.  void startPrefixMapping(String prefix, String uri) - Called when a new namespace mapping is defined.  void endPrefixMapping(String prefix) - Called when a namespace definition ends its scope.
  • 24. Attributes Interface This interface specifies methods for processing the attributes connected to an element.  int getLength() - Returns number of attributes.  String getQName(int index)  String getValue(int index)  String getValue(String qname)
  • 25. Parsing XML document import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class UserHandler extends DefaultHandler { boolean bFirstName = false; boolean bLastName = false; boolean bNickName = false; boolean bMarks = false;
  • 26. Parsing XML document public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("student")) { String rollNo = attributes.getValue("rollno"); System.out.println("Roll No : " + rollNo); } else if (qName.equalsIgnoreCase("firstname")) { bFirstName = true; } else if (qName.equalsIgnoreCase("lastname")) { bLastName = true; } else if (qName.equalsIgnoreCase("nickname")) { bNickName = true; } else if (qName.equalsIgnoreCase("marks")) { bMarks = true; } };
  • 27. Parsing XML document public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("student")) { System.out.println("End Element :" + qName); } }
  • 28. Parsing XML document public void characters(char ch[], int start, int length) throws SAXException) { if (bFirstName) { System.out.println("First Name: " + new String(ch, start, length)); bFirstName = false; } else if (bLastName) { System.out.println("Last Name: " + new String(ch, start, length)); bLastName = false; } else if (bNickName) { System.out.println("Nick Name: " + new String(ch, start, length)); bNickName = false; } else if (bMarks)
  • 29. Parsing XML document public class SAXParserDemo { public static void main(String[] args) { try { File inputFile = new File("input.txt"); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); UserHandler userhandler = new UserHandler(); saxParser.parse(inputFile, userhandler); } catch (Exception e) { e.printStackTrace(); } } }