SlideShare a Scribd company logo
SAX
Dr.S.Roselin Mary
HOD/CSE
ANAND INSTITUTE OF HIGHER TECHNOLOGY
• SAX (Simple API for XML) is an event-based parser for XML
documents.
• Unlike a DOM parser, a 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.
– Reads an XML document from top to bottom, recognizing the
tokens that make up a well-formed XML document.
– Tokens are processed in the same order that they appear in the
document.
– Reports the application program the nature of tokens that the
parser has encountered as they occur.
– The application program provides an "event" handler that must
be registered with the parser.
– As the tokens are identified, callback methods in the handler are
invoked with the relevant information.7/16/2019 2Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
Why Do I Need SAX?
• to pull out the text from a document or to look for attributes of
specific tags, we might be able to do some of the work using a tool
or maybe XSLT, but these solutions have their limitations.
• writing a tool or a standalone program to process XML, SAX is a
good way to do it.
• Many applications today can be customized using an XML file.
These files have replaced the traditional “properties” files for
reasons of uniformity and richness of expression.
• Instead of spending a lot of your time writing a parser to read XML
files, you might as well use SAX.
• SAX is completely free, so it can be embedded in a larger
application without royalty fees or even copyright notices.
• Some SAX parsers can validate a document against a Document
Type Definition (DTD).
• Validating parsers can also tell specifically where validation has
failed.
7/16/2019 3Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
SAX Parser
• When should I use it?
– Large documents
– Memory constrained devices
• When should I use something else?
– If you need to modify the document
– SAX doesn’t remember previous events unless you
write explicit code to do so.
7/16/2019 4Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
SAX Parser
• Which languages are supported?
– Java
– Perl
– C++
– Python
7/16/2019 5Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
SAX Parser
• Versions
– SAX 1 introduced in May 1998
– SAX 2.0 introduced in May 2000 and adds support
for
• namespaces
• filter chains
• querying and setting properties in the parser
7/16/2019 6Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
SAX Parser
• Some popular SAX APIs
– Apache XML Project Xerces Java Parser
http://xml.apache.org/xerces-j/index.html
– IBM’s XML for Java (XML4J)
http://www.alphaworks.ibm.com/formula/xml
– For a complete list, see
http://www.megginson.com/SAX
7/16/2019 7Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
DOM SAX
Tree model parser (Object based) (Tree of
nodes).
Event based parser (Sequence of events).
DOM loads the file into the memory and
then parse- the file
SAX parses the file as it reads it, i.e. parses
node by node.
Has memory constraints since it loads the
whole XML file before parsing.
No memory constraints as it does not
store the XML content in the memory.
DOM is read and write (can insert or
delete nodes).
SAX is read only i.e. can’t insert or delete
the node
If the XML content is small, then prefer
DOM parser.
Use SAX parser when memory content is
large.
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.
Slower at run time. Faster at run time.
7/16/2019 8Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
SAX Basics
• build a content handler by creating a Java
class that implements the ContentHandler
interface in the org.xml.sax package.
• Once we have a content handler, simply
register it with a SAX XMLReader.
• set up the input source, and start the parser.
• the methods in our content handler will be
called when the parser encounters elements,
text, and other data.
7/16/2019 9Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
<?xml version=”1.0” encoding=”UTF-8”?>
<fiction>
<book author=”Herman Melville”>Moby Dick</book>
</fiction>
Generated Events:
start document
start element: fiction
start element: book (including attributes)
characters: Moby Dick
end element: book
end element: fiction
end document
7/16/2019 10Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
SAX Packages
• The SAX 2.0 API consists of two standard
packages and one extension package.
• The standard packages
– org.xml.sax
– org.xml.helpers.
• The org.xml.sax package contains the basic
classes, interfaces, and exceptions needed for
parsing documents.
7/16/2019 11Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
The org.xml.sax Package
Name Description
AttributeList This interface has been replaced by the SAX2
Attributes interface, which includes namespace support.
Attributes Interface for a list of XML attributes.
ContentHandler Receives notification of the logical content of a document.
DocumentHandler Deprecated. This interface has been replaced by the SAX2
ContentHandler interface, which includes namespace support.
DTDHandler Receives notification of basic DTD-related events.
EntityResolver Basic interface for resolving entities.
ErrorHandler Basic interface for SAX error handlers.
Locator Interface for associating a SAX event with a document Location.
Parser Deprecated. This interface has been replaced by the SAX2
XMLReader interface, which includes namespace support.
XMLFilter Interface for an XML filter.
XMLReader Interface for reading an XML document using callbacks.
Interfaces
7/16/2019 12
Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
HandlerBase Deprecated. This class works with the deprecated
DocumentHandler interface.
InputSource A single input source for an XML entity.
SAXException Encapsulates a general SAX error or warning.
SAXNotRecognizedException Exception class for an unrecognized identifier.
SAXNotSupportedException Exception class for an unsupported operation.
SAXParseException Encapsulates an XML parse error or warning.
Classes
Exceptions
7/16/2019 13Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
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.
void endElement(String uri, String localName,String qName) − Called at the end of an
element.
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.
7/16/2019 14Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
What is SAX Parsing?
• Callback Methods
– The SAX API has a default handler class built in so
you don’t have to re-implement the interfaces every
time (org.xml.sax.helpers.DefaultHandler)
– The five most common methods to override are:
• startElement(String uri, String lname, String qname,
Attributes atts)
• endDocument(String uri, String lname, String qname)
• characters(char text[], int start, int length)
• startDocument()
• endDocument()
7/16/2019 15Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
• startElement()
– Four parameters:
• String uri = the namespace URI (Uniform Resource Identifier)
• String lname = the local name of the element
• String qname = the qualified name of the element
• Attributes atts = list of attributes for this element
– If the current element is a complex element, an object of
the appropriate type is created and pushed on to the stack
– If the element is simple, a StringBuffer is pushed on to the
stack, ready to accept character data
7/16/2019 16Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
• endElement()
– Three parameters:
• String uri = the namespace URI (Uniform Resource Identifier)
• String lname = the local name of the element
• String qname = the qualified name of the element
– The topmost element on the stack is popped,
converted to the proper type, and inserted into its
parent, which now occupies the top of the stack
(unless this is the root element – special handling
required)
7/16/2019 17Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
• characters()
– Three parameters:
• char text[] = character array containing the entire XML
document
• int start = starting index of current data in text[]
• int length = ending index of current data in text[]
– When the parser encounters raw text, it passes a char
array containing the actual data, the starting position,
and the length of data to be read from the array
– The implementation of the callback method inserts
the data into the StringBuffer located on the top of the
stack
– Can lead to confusion because of:
• No guarantee that a single stretch of characters results in
one call to characters()
• It stores all characters, including whitespace, encountered by
the parser
7/16/2019 18Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
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)
7/16/2019 19Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
• The org.xml.sax.helpers package contains
additional classes that can simplify some of
coding and make it more portable.
• We can find a number of adapters that
implement many of the handler interfaces, so
you don’t need to fill in all the methods
defined in the interfaces.
7/16/2019 20Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
7/16/2019 21Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
• The org.xml.sax.ext package is an extension
that is not shipped with all implementations.
• It contains two handler interfaces for
capturing declaration and lexical events
7/16/2019 22Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
To list all the student elements details including attribute & Child
import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserDemo
{
public static void main(String[] args)
{
try
{
File inputFile = new File(“src/students.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
UserHandler userhandler = new UserHandler();
saxParser.parse(inputFile, userhandler);
}
catch (Exception e) { e.printStackTrace(); }
}
}
7/16/2019 23
Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
class UserHandler extends DefaultHandler
{
boolean bname = false;
boolean bgender = false;
boolean bmarks = false;
@Override
public void startElement( String uri, String localName, String qName,
Attributes attributes)
throws SAXException
{
if (qName.equalsIgnoreCase("student"))
{
String rollNo = attributes.getValue(“id");
System.out.println(“id : " + rollNo);
}
else if (qName.equalsIgnoreCase("name"))
{
bname = true;
}
else if (qName.equalsIgnoreCase(“gender"))
{
bgender = true;
}
else if (qName.equalsIgnoreCase(“marks"))
{
bmarks = true;
7/16/2019 24
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if (qName.equalsIgnoreCase("student"))
{ System.out.println("End Element :" + qName); }
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException
{
if (bname)
{
System.out.println("Name: " + new String(ch, start, length));
bname = false;
}
else if (bgender)
{
System.out.println(“Gender: " + new String(ch, start, length));
bgender = false;
}
else if (bmarks) {
System.out.println("Marks: " + new String(ch, start, length));
bmarks = false;
}7/16/2019 25
SAXQueryDemo.java
package com.tutorialspoint.xml;
import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXQueryDemo
{
public static void main(String[] args)
{
try
{
File inputFile = new File("src/students.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
UserHandler userhandler = new UserHandler();
saxParser.parse(inputFile, userhandler);
}
catch (Exception e) { e.printStackTrace(); }
}
}7/16/2019 26Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
class UserHandler extends DefaultHandler
{
boolean bname = false;
boolean bgender = false;
boolean bmarks = false;
String rollNo = null;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if (qName.equalsIgnoreCase("student"))
{ rollNo = attributes.getValue("id");}
if(("001").equals(rollNo) && qName.equalsIgnoreCase("student"))
{ System.out.println("Start Element :" + qName); }
if (qName.equalsIgnoreCase("name")) { bname = true;}
else if (qName.equalsIgnoreCase("gender")) { bgender = true;}
else if (qName.equalsIgnoreCase("marks")) { bmarks = true; }
}
7/16/2019 27Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if (qName.equalsIgnoreCase("student"))
{
if(("001").equals(rollNo) && qName.equalsIgnoreCase("student"))
System.out.println("End Element :" + qName);
}
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException
{
if (bname && ("001").equals(rollNo))
{
System.out.println(" Name: " + new String(ch, start, length));
bname = false;
}
else if (bgender && ("001").equals(rollNo))
{
System.out.println("Gender: " + new String(ch, start, length));
bgender = false;
}
else if (bmarks && ("001").equals(rollNo))
{
System.out.println("Marks: " + new String(ch, start, length));
Validation
• SAX parsers come in two varieties: validating and
nonvalidating.
– Validating parsers can determine whether an XML document is
valid based on a Document Type Definition (DTD) or Schema.
– The SAX parser shipped with Apache Xerces is a validating
parser.
• In order to use validation, we must turn it on by setting the
validation feature to true.
• If we try to turn on validation with a nonvalidating parser, a
SAXNotSupportedException will bethrown.
• If the parser does not recognize the feature, a
SAXNotRecognizedException will be thrown. This helps in
determining whether we mistyped the feature name.
7/16/2019 29Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
• ErrorHandler contains three methods that can
be used to determine whether a document is
well formed and valid.
• Either error() or warning() will be called if the
document is well formed but not valid(that is,
it violates the rules of the DTD), and
fatalError() will be called if the document is
not well formed.
7/16/2019 30Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
Validator
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class SAXValidator extends DefaultHandler
{
private boolean valid;
private boolean wellFormed;
public SAXValidator() { valid = true; wellFormed = true;}
public void startDocument() { System.out.println(“***Start of Document***”);}
public void endDocument() {System.out.println(“***End of Document***”);}
public void error(SAXParseException e) { valid = false;}
public void fatalError(SAXParseException e) {wellFormed = false;}
7/16/2019 31Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
public void warning(SAXParseException e) {valid = false;}
public boolean isValid() {return valid;}
public boolean isWellFormed() { return wellFormed;}
public static void main(String args[]) throws Exception
{
if (args.length != 1)
{
System.err.println(“Usage: java SAXValidate <xml-file>”);
System.exit(1);
}
XMLReader parser =
XMLReaderFactory.createXMLReader(“org.apache.xerces.parsers.SAXParser”);
parser.setFeature(“http://xml.org/sax/features/validation”, true);
7/16/2019 32Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
SAXValidator handler = new SAXValidator();
parser.setContentHandler(handler);
parser.setErrorHandler(handler);
parser.parse(new InputSource(new FileReader(args[0])));
if (!handler.isWellFormed()) {System.out.println(“Document is NOT well formed.”);}
if (!handler.isValid()) { System.out.println(“Document is NOT valid.”);}
if (handler.isWellFormed() && handler.isValid())
{ System.out.println(“Document is well formed and valid.”);}
}
}
7/16/2019 33Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
Lexical Events
• We can receive the events comments, CDATA,
and DTD references using an extension
interface called LexicalHandler.
• LexicalHandler is part of the org.xml.sax.ext
package, which is not necessarily supported
by all SAX implementations.
• Xerces provides support for the extension
package.
7/16/2019 34Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
7/16/2019 35Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;
public class SAXLexical extends DefaultHandler implements LexicalHandler
{
public SAXLexical() { }
public void startDocument() { System.out.println(“***Start of Document***”); }
public void endDocument() { System.out.println(“***End of Document***”); }
public void startElement(String uri,String localName,String qName,Attributes attribute
{
System.out.print(“<” + qName);
int n = attributes.getLength();
for (int i=0; i<n; i+=1)
{ System.out.print(“ “ + attributes.getQName(i) +“=’” + attributes.getValue(i) + “‘“);}
System.out.println(“>”);
}
Lexical Event
7/16/2019 36
Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
public void characters(char[] ch, int start, int length)
{ System.out.println(new String(ch, start, length).trim()); }
public void endElement(String namespaceURI, String localName,String qName)
throws SAXException
{ System.out.println(“</” + qName + “>”); }
public void startDTD(String name, String publicId, String systemId)
throws SAXException
{
System.out.print(“*** Start DTD, name “ + name);
if (publicId != null) { System.out.print(“ PUBLIC “ + publicId); }
if (systemId != null) { System.out.print(“ SYSTEM “ + systemId); }
System.out.println(“ ***”);
}
public void endDTD() throws SAXException {
System.out.println(“*** End DTD ***”);
}
public void startEntity(String name) throws SAXException {
System.out.println(“*** Start Entity “ + name + “ ***”);
} 7/16/2019 37
Dr.S.ROSELIN MARY HOD/CSE , ANAND
INSTITUTE OF HIGHER TECHNOLOGY
public void endEntity(String name) throws SAXException
{ System.out.println(“*** End Entity “ + name + “ ***”); }
public void startCDATA() throws SAXException
{ System.out.println(“*** Start CDATA ***”);}
public void endCDATA() throws SAXException
{ System.out.println(“*** End CDATA ***”); }
public void comment(char[] ch, int start, int length) throws SAXException
{
System.out.println(“<!— “ +new String(ch, start, length) + “ —>”);
}
public static void main(String args[]) throws Exception
{
if (args.length != 1)
{
System.err.println(“Usage: java SAXLexical <xml-file>”);
System.exit(1);
}
7/16/2019 38
Dr.S.ROSELIN MARY HOD/CSE , ANAND
INSTITUTE OF HIGHER TECHNOLOGY

More Related Content

What's hot

XML Schema
XML SchemaXML Schema
XML Schema
yht4ever
 
Css Ppt
Css PptCss Ppt
Css Ppt
Hema Prasanth
 
XML
XMLXML
Understanding Web Cache
Understanding Web CacheUnderstanding Web Cache
Understanding Web Cache
ProdigyView
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
BOSS Webtech
 
Simple object access protocol(soap )
Simple object access protocol(soap )Simple object access protocol(soap )
Simple object access protocol(soap )
balamurugan.k Kalibalamurugan
 
Xml schema
Xml schemaXml schema
Xml schema
Prabhakaran V M
 
Restful web services ppt
Restful web services pptRestful web services ppt
XML
XMLXML
The Semantic Web #9 - Web Ontology Language (OWL)
The Semantic Web #9 - Web Ontology Language (OWL)The Semantic Web #9 - Web Ontology Language (OWL)
The Semantic Web #9 - Web Ontology Language (OWL)
Myungjin Lee
 
Semantic web
Semantic web Semantic web
Semantic web
Pallavi Srivastava
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
Jussi Pohjolainen
 
DOM and SAX
DOM and SAXDOM and SAX
DOM and SAX
Jussi Pohjolainen
 
Xml
XmlXml
Images and Tables in HTML
Images and Tables in HTMLImages and Tables in HTML
Images and Tables in HTML
Aarti P
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
Mindy McAdams
 
DTD
DTDDTD
Fundamentals of Web for Non-Developers
Fundamentals of Web for Non-DevelopersFundamentals of Web for Non-Developers
Fundamentals of Web for Non-Developers
Lemi Orhan Ergin
 
Document object model(dom)
Document object model(dom)Document object model(dom)
Document object model(dom)
rahul kundu
 
Css3
Css3Css3

What's hot (20)

XML Schema
XML SchemaXML Schema
XML Schema
 
Css Ppt
Css PptCss Ppt
Css Ppt
 
XML
XMLXML
XML
 
Understanding Web Cache
Understanding Web CacheUnderstanding Web Cache
Understanding Web Cache
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
 
Simple object access protocol(soap )
Simple object access protocol(soap )Simple object access protocol(soap )
Simple object access protocol(soap )
 
Xml schema
Xml schemaXml schema
Xml schema
 
Restful web services ppt
Restful web services pptRestful web services ppt
Restful web services ppt
 
XML
XMLXML
XML
 
The Semantic Web #9 - Web Ontology Language (OWL)
The Semantic Web #9 - Web Ontology Language (OWL)The Semantic Web #9 - Web Ontology Language (OWL)
The Semantic Web #9 - Web Ontology Language (OWL)
 
Semantic web
Semantic web Semantic web
Semantic web
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
DOM and SAX
DOM and SAXDOM and SAX
DOM and SAX
 
Xml
XmlXml
Xml
 
Images and Tables in HTML
Images and Tables in HTMLImages and Tables in HTML
Images and Tables in HTML
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
DTD
DTDDTD
DTD
 
Fundamentals of Web for Non-Developers
Fundamentals of Web for Non-DevelopersFundamentals of Web for Non-Developers
Fundamentals of Web for Non-Developers
 
Document object model(dom)
Document object model(dom)Document object model(dom)
Document object model(dom)
 
Css3
Css3Css3
Css3
 

Similar to Service Oriented Architecture - Unit II - Sax

Unit 2.3
Unit 2.3Unit 2.3
Unit 2.3
Unit 2.3Unit 2.3
Xml parsing
Xml parsingXml parsing
Xml parsing
Malintha Adikari
 
Sax parser
Sax parserSax parser
Sax parser
Mahara Jothi
 
XML
XMLXML
XMl
XMlXMl
Python xml processing
Python   xml processingPython   xml processing
Python xml processing
Learnbay Datascience
 
XML - SAX
XML - SAXXML - SAX
Sax Dom Tutorial
Sax Dom TutorialSax Dom Tutorial
Sax Dom Tutorial
vikram singh
 
Java XML Parsing
Java XML ParsingJava XML Parsing
Java XML Parsing
srinivasanjayakumar
 
JAXP
JAXPJAXP
uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
Kumar
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
Himanshu Soni
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
Himanshu Soni
 
Xml3
Xml3Xml3
Using elasticsearch with rails
Using elasticsearch with railsUsing elasticsearch with rails
Using elasticsearch with rails
Tom Z Zeng
 
1 xml fundamentals
1 xml fundamentals1 xml fundamentals
1 xml fundamentals
Dr.Saranya K.G
 
XML
XMLXML
SAX PARSER
SAX PARSER SAX PARSER
SAX PARSER
Saranya Arunprasath
 

Similar to Service Oriented Architecture - Unit II - Sax (20)

Unit 2.3
Unit 2.3Unit 2.3
Unit 2.3
 
Unit 2.3
Unit 2.3Unit 2.3
Unit 2.3
 
Xml parsing
Xml parsingXml parsing
Xml parsing
 
Sax parser
Sax parserSax parser
Sax parser
 
XML
XMLXML
XML
 
XMl
XMlXMl
XMl
 
Python xml processing
Python   xml processingPython   xml processing
Python xml processing
 
XML - SAX
XML - SAXXML - SAX
XML - SAX
 
Sax Dom Tutorial
Sax Dom TutorialSax Dom Tutorial
Sax Dom Tutorial
 
Java XML Parsing
Java XML ParsingJava XML Parsing
Java XML Parsing
 
JAXP
JAXPJAXP
JAXP
 
uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
 
Xml3
Xml3Xml3
Xml3
 
Using elasticsearch with rails
Using elasticsearch with railsUsing elasticsearch with rails
Using elasticsearch with rails
 
1 xml fundamentals
1 xml fundamentals1 xml fundamentals
1 xml fundamentals
 
XML
XMLXML
XML
 
SAX PARSER
SAX PARSER SAX PARSER
SAX PARSER
 

More from Roselin Mary S

Unit 2 hci in software process
Unit 2   hci in software processUnit 2   hci in software process
Unit 2 hci in software process
Roselin Mary S
 
Unit1 17-08-2020 HUMAN COMPUTER INTERACTION
Unit1  17-08-2020 HUMAN COMPUTER INTERACTIONUnit1  17-08-2020 HUMAN COMPUTER INTERACTION
Unit1 17-08-2020 HUMAN COMPUTER INTERACTION
Roselin Mary S
 
Unit 1 defects classes
Unit 1 defects classesUnit 1 defects classes
Unit 1 defects classes
Roselin Mary S
 
Unit 1 part 2
Unit 1 part 2Unit 1 part 2
Unit 1 part 2
Roselin Mary S
 
IT 8076 Software Testing Unit1
IT 8076 Software Testing Unit1IT 8076 Software Testing Unit1
IT 8076 Software Testing Unit1
Roselin Mary S
 
Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml
Roselin Mary S
 
Service Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIService Oriented Architecture - Unit II
Service Oriented Architecture - Unit II
Roselin Mary S
 
Service Oriented Architecture- UNIT 2- XSL
Service Oriented Architecture- UNIT 2- XSLService Oriented Architecture- UNIT 2- XSL
Service Oriented Architecture- UNIT 2- XSL
Roselin Mary S
 
Service oriented architeture Unit 1
Service oriented architeture  Unit 1Service oriented architeture  Unit 1
Service oriented architeture Unit 1
Roselin Mary S
 

More from Roselin Mary S (9)

Unit 2 hci in software process
Unit 2   hci in software processUnit 2   hci in software process
Unit 2 hci in software process
 
Unit1 17-08-2020 HUMAN COMPUTER INTERACTION
Unit1  17-08-2020 HUMAN COMPUTER INTERACTIONUnit1  17-08-2020 HUMAN COMPUTER INTERACTION
Unit1 17-08-2020 HUMAN COMPUTER INTERACTION
 
Unit 1 defects classes
Unit 1 defects classesUnit 1 defects classes
Unit 1 defects classes
 
Unit 1 part 2
Unit 1 part 2Unit 1 part 2
Unit 1 part 2
 
IT 8076 Software Testing Unit1
IT 8076 Software Testing Unit1IT 8076 Software Testing Unit1
IT 8076 Software Testing Unit1
 
Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml
 
Service Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIService Oriented Architecture - Unit II
Service Oriented Architecture - Unit II
 
Service Oriented Architecture- UNIT 2- XSL
Service Oriented Architecture- UNIT 2- XSLService Oriented Architecture- UNIT 2- XSL
Service Oriented Architecture- UNIT 2- XSL
 
Service oriented architeture Unit 1
Service oriented architeture  Unit 1Service oriented architeture  Unit 1
Service oriented architeture Unit 1
 

Recently uploaded

ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 

Recently uploaded (20)

ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 

Service Oriented Architecture - Unit II - Sax

  • 2. • SAX (Simple API for XML) is an event-based parser for XML documents. • Unlike a DOM parser, a 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. – Reads an XML document from top to bottom, recognizing the tokens that make up a well-formed XML document. – Tokens are processed in the same order that they appear in the document. – Reports the application program the nature of tokens that the parser has encountered as they occur. – The application program provides an "event" handler that must be registered with the parser. – As the tokens are identified, callback methods in the handler are invoked with the relevant information.7/16/2019 2Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 3. Why Do I Need SAX? • to pull out the text from a document or to look for attributes of specific tags, we might be able to do some of the work using a tool or maybe XSLT, but these solutions have their limitations. • writing a tool or a standalone program to process XML, SAX is a good way to do it. • Many applications today can be customized using an XML file. These files have replaced the traditional “properties” files for reasons of uniformity and richness of expression. • Instead of spending a lot of your time writing a parser to read XML files, you might as well use SAX. • SAX is completely free, so it can be embedded in a larger application without royalty fees or even copyright notices. • Some SAX parsers can validate a document against a Document Type Definition (DTD). • Validating parsers can also tell specifically where validation has failed. 7/16/2019 3Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 4. SAX Parser • When should I use it? – Large documents – Memory constrained devices • When should I use something else? – If you need to modify the document – SAX doesn’t remember previous events unless you write explicit code to do so. 7/16/2019 4Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 5. SAX Parser • Which languages are supported? – Java – Perl – C++ – Python 7/16/2019 5Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 6. SAX Parser • Versions – SAX 1 introduced in May 1998 – SAX 2.0 introduced in May 2000 and adds support for • namespaces • filter chains • querying and setting properties in the parser 7/16/2019 6Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 7. SAX Parser • Some popular SAX APIs – Apache XML Project Xerces Java Parser http://xml.apache.org/xerces-j/index.html – IBM’s XML for Java (XML4J) http://www.alphaworks.ibm.com/formula/xml – For a complete list, see http://www.megginson.com/SAX 7/16/2019 7Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 8. DOM SAX Tree model parser (Object based) (Tree of nodes). Event based parser (Sequence of events). DOM loads the file into the memory and then parse- the file SAX parses the file as it reads it, i.e. parses node by node. Has memory constraints since it loads the whole XML file before parsing. No memory constraints as it does not store the XML content in the memory. DOM is read and write (can insert or delete nodes). SAX is read only i.e. can’t insert or delete the node If the XML content is small, then prefer DOM parser. Use SAX parser when memory content is large. 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. Slower at run time. Faster at run time. 7/16/2019 8Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 9. SAX Basics • build a content handler by creating a Java class that implements the ContentHandler interface in the org.xml.sax package. • Once we have a content handler, simply register it with a SAX XMLReader. • set up the input source, and start the parser. • the methods in our content handler will be called when the parser encounters elements, text, and other data. 7/16/2019 9Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 10. <?xml version=”1.0” encoding=”UTF-8”?> <fiction> <book author=”Herman Melville”>Moby Dick</book> </fiction> Generated Events: start document start element: fiction start element: book (including attributes) characters: Moby Dick end element: book end element: fiction end document 7/16/2019 10Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 11. SAX Packages • The SAX 2.0 API consists of two standard packages and one extension package. • The standard packages – org.xml.sax – org.xml.helpers. • The org.xml.sax package contains the basic classes, interfaces, and exceptions needed for parsing documents. 7/16/2019 11Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 12. The org.xml.sax Package Name Description AttributeList This interface has been replaced by the SAX2 Attributes interface, which includes namespace support. Attributes Interface for a list of XML attributes. ContentHandler Receives notification of the logical content of a document. DocumentHandler Deprecated. This interface has been replaced by the SAX2 ContentHandler interface, which includes namespace support. DTDHandler Receives notification of basic DTD-related events. EntityResolver Basic interface for resolving entities. ErrorHandler Basic interface for SAX error handlers. Locator Interface for associating a SAX event with a document Location. Parser Deprecated. This interface has been replaced by the SAX2 XMLReader interface, which includes namespace support. XMLFilter Interface for an XML filter. XMLReader Interface for reading an XML document using callbacks. Interfaces 7/16/2019 12 Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 13. HandlerBase Deprecated. This class works with the deprecated DocumentHandler interface. InputSource A single input source for an XML entity. SAXException Encapsulates a general SAX error or warning. SAXNotRecognizedException Exception class for an unrecognized identifier. SAXNotSupportedException Exception class for an unsupported operation. SAXParseException Encapsulates an XML parse error or warning. Classes Exceptions 7/16/2019 13Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 14. 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. void endElement(String uri, String localName,String qName) − Called at the end of an element. 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. 7/16/2019 14Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 15. What is SAX Parsing? • Callback Methods – The SAX API has a default handler class built in so you don’t have to re-implement the interfaces every time (org.xml.sax.helpers.DefaultHandler) – The five most common methods to override are: • startElement(String uri, String lname, String qname, Attributes atts) • endDocument(String uri, String lname, String qname) • characters(char text[], int start, int length) • startDocument() • endDocument() 7/16/2019 15Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 16. • startElement() – Four parameters: • String uri = the namespace URI (Uniform Resource Identifier) • String lname = the local name of the element • String qname = the qualified name of the element • Attributes atts = list of attributes for this element – If the current element is a complex element, an object of the appropriate type is created and pushed on to the stack – If the element is simple, a StringBuffer is pushed on to the stack, ready to accept character data 7/16/2019 16Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 17. • endElement() – Three parameters: • String uri = the namespace URI (Uniform Resource Identifier) • String lname = the local name of the element • String qname = the qualified name of the element – The topmost element on the stack is popped, converted to the proper type, and inserted into its parent, which now occupies the top of the stack (unless this is the root element – special handling required) 7/16/2019 17Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 18. • characters() – Three parameters: • char text[] = character array containing the entire XML document • int start = starting index of current data in text[] • int length = ending index of current data in text[] – When the parser encounters raw text, it passes a char array containing the actual data, the starting position, and the length of data to be read from the array – The implementation of the callback method inserts the data into the StringBuffer located on the top of the stack – Can lead to confusion because of: • No guarantee that a single stretch of characters results in one call to characters() • It stores all characters, including whitespace, encountered by the parser 7/16/2019 18Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 19. 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) 7/16/2019 19Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 20. • The org.xml.sax.helpers package contains additional classes that can simplify some of coding and make it more portable. • We can find a number of adapters that implement many of the handler interfaces, so you don’t need to fill in all the methods defined in the interfaces. 7/16/2019 20Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 21. 7/16/2019 21Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 22. • The org.xml.sax.ext package is an extension that is not shipped with all implementations. • It contains two handler interfaces for capturing declaration and lexical events 7/16/2019 22Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 23. To list all the student elements details including attribute & Child import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SAXParserDemo { public static void main(String[] args) { try { File inputFile = new File(“src/students.xml"); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); UserHandler userhandler = new UserHandler(); saxParser.parse(inputFile, userhandler); } catch (Exception e) { e.printStackTrace(); } } } 7/16/2019 23 Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 24. class UserHandler extends DefaultHandler { boolean bname = false; boolean bgender = false; boolean bmarks = false; @Override public void startElement( String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("student")) { String rollNo = attributes.getValue(“id"); System.out.println(“id : " + rollNo); } else if (qName.equalsIgnoreCase("name")) { bname = true; } else if (qName.equalsIgnoreCase(“gender")) { bgender = true; } else if (qName.equalsIgnoreCase(“marks")) { bmarks = true; 7/16/2019 24
  • 25. @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("student")) { System.out.println("End Element :" + qName); } } @Override public void characters(char ch[], int start, int length) throws SAXException { if (bname) { System.out.println("Name: " + new String(ch, start, length)); bname = false; } else if (bgender) { System.out.println(“Gender: " + new String(ch, start, length)); bgender = false; } else if (bmarks) { System.out.println("Marks: " + new String(ch, start, length)); bmarks = false; }7/16/2019 25
  • 26. SAXQueryDemo.java package com.tutorialspoint.xml; import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SAXQueryDemo { public static void main(String[] args) { try { File inputFile = new File("src/students.xml"); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); UserHandler userhandler = new UserHandler(); saxParser.parse(inputFile, userhandler); } catch (Exception e) { e.printStackTrace(); } } }7/16/2019 26Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 27. class UserHandler extends DefaultHandler { boolean bname = false; boolean bgender = false; boolean bmarks = false; String rollNo = null; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("student")) { rollNo = attributes.getValue("id");} if(("001").equals(rollNo) && qName.equalsIgnoreCase("student")) { System.out.println("Start Element :" + qName); } if (qName.equalsIgnoreCase("name")) { bname = true;} else if (qName.equalsIgnoreCase("gender")) { bgender = true;} else if (qName.equalsIgnoreCase("marks")) { bmarks = true; } } 7/16/2019 27Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 28. @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("student")) { if(("001").equals(rollNo) && qName.equalsIgnoreCase("student")) System.out.println("End Element :" + qName); } } @Override public void characters(char ch[], int start, int length) throws SAXException { if (bname && ("001").equals(rollNo)) { System.out.println(" Name: " + new String(ch, start, length)); bname = false; } else if (bgender && ("001").equals(rollNo)) { System.out.println("Gender: " + new String(ch, start, length)); bgender = false; } else if (bmarks && ("001").equals(rollNo)) { System.out.println("Marks: " + new String(ch, start, length));
  • 29. Validation • SAX parsers come in two varieties: validating and nonvalidating. – Validating parsers can determine whether an XML document is valid based on a Document Type Definition (DTD) or Schema. – The SAX parser shipped with Apache Xerces is a validating parser. • In order to use validation, we must turn it on by setting the validation feature to true. • If we try to turn on validation with a nonvalidating parser, a SAXNotSupportedException will bethrown. • If the parser does not recognize the feature, a SAXNotRecognizedException will be thrown. This helps in determining whether we mistyped the feature name. 7/16/2019 29Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 30. • ErrorHandler contains three methods that can be used to determine whether a document is well formed and valid. • Either error() or warning() will be called if the document is well formed but not valid(that is, it violates the rules of the DTD), and fatalError() will be called if the document is not well formed. 7/16/2019 30Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 31. Validator import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class SAXValidator extends DefaultHandler { private boolean valid; private boolean wellFormed; public SAXValidator() { valid = true; wellFormed = true;} public void startDocument() { System.out.println(“***Start of Document***”);} public void endDocument() {System.out.println(“***End of Document***”);} public void error(SAXParseException e) { valid = false;} public void fatalError(SAXParseException e) {wellFormed = false;} 7/16/2019 31Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 32. public void warning(SAXParseException e) {valid = false;} public boolean isValid() {return valid;} public boolean isWellFormed() { return wellFormed;} public static void main(String args[]) throws Exception { if (args.length != 1) { System.err.println(“Usage: java SAXValidate <xml-file>”); System.exit(1); } XMLReader parser = XMLReaderFactory.createXMLReader(“org.apache.xerces.parsers.SAXParser”); parser.setFeature(“http://xml.org/sax/features/validation”, true); 7/16/2019 32Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 33. SAXValidator handler = new SAXValidator(); parser.setContentHandler(handler); parser.setErrorHandler(handler); parser.parse(new InputSource(new FileReader(args[0]))); if (!handler.isWellFormed()) {System.out.println(“Document is NOT well formed.”);} if (!handler.isValid()) { System.out.println(“Document is NOT valid.”);} if (handler.isWellFormed() && handler.isValid()) { System.out.println(“Document is well formed and valid.”);} } } 7/16/2019 33Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 34. Lexical Events • We can receive the events comments, CDATA, and DTD references using an extension interface called LexicalHandler. • LexicalHandler is part of the org.xml.sax.ext package, which is not necessarily supported by all SAX implementations. • Xerces provides support for the extension package. 7/16/2019 34Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 35. 7/16/2019 35Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 36. import java.io.*; import org.xml.sax.*; import org.xml.sax.ext.*; import org.xml.sax.helpers.*; public class SAXLexical extends DefaultHandler implements LexicalHandler { public SAXLexical() { } public void startDocument() { System.out.println(“***Start of Document***”); } public void endDocument() { System.out.println(“***End of Document***”); } public void startElement(String uri,String localName,String qName,Attributes attribute { System.out.print(“<” + qName); int n = attributes.getLength(); for (int i=0; i<n; i+=1) { System.out.print(“ “ + attributes.getQName(i) +“=’” + attributes.getValue(i) + “‘“);} System.out.println(“>”); } Lexical Event 7/16/2019 36 Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 37. public void characters(char[] ch, int start, int length) { System.out.println(new String(ch, start, length).trim()); } public void endElement(String namespaceURI, String localName,String qName) throws SAXException { System.out.println(“</” + qName + “>”); } public void startDTD(String name, String publicId, String systemId) throws SAXException { System.out.print(“*** Start DTD, name “ + name); if (publicId != null) { System.out.print(“ PUBLIC “ + publicId); } if (systemId != null) { System.out.print(“ SYSTEM “ + systemId); } System.out.println(“ ***”); } public void endDTD() throws SAXException { System.out.println(“*** End DTD ***”); } public void startEntity(String name) throws SAXException { System.out.println(“*** Start Entity “ + name + “ ***”); } 7/16/2019 37 Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY
  • 38. public void endEntity(String name) throws SAXException { System.out.println(“*** End Entity “ + name + “ ***”); } public void startCDATA() throws SAXException { System.out.println(“*** Start CDATA ***”);} public void endCDATA() throws SAXException { System.out.println(“*** End CDATA ***”); } public void comment(char[] ch, int start, int length) throws SAXException { System.out.println(“<!— “ +new String(ch, start, length) + “ —>”); } public static void main(String args[]) throws Exception { if (args.length != 1) { System.err.println(“Usage: java SAXLexical <xml-file>”); System.exit(1); } 7/16/2019 38 Dr.S.ROSELIN MARY HOD/CSE , ANAND INSTITUTE OF HIGHER TECHNOLOGY