0% found this document useful (0 votes)
61 views

SAX (Simple API For XML)

SAX (Simple API for XML) is an event-based parser that reads XML documents sequentially from top to bottom. It generates callback events as it encounters tokens in the document and does not build an in-memory DOM tree. SAX is faster and more memory efficient than DOM for parsing large files but offers less random access to the document contents. The application registers handler methods that are called when specific tokens like start tags or character data are encountered.

Uploaded by

Sheeba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

SAX (Simple API For XML)

SAX (Simple API for XML) is an event-based parser that reads XML documents sequentially from top to bottom. It generates callback events as it encounters tokens in the document and does not build an in-memory DOM tree. SAX is faster and more memory efficient than DOM for parsing large files but offers less random access to the document contents. The application registers handler methods that are called when specific tokens like start tags or character data are encountered.

Uploaded by

Sheeba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

SAX (Simple API for XML)

SAX (Simple API for XML)


• SAX, also known as the Simple API for XML, is used
for parsing XML documents.
• It is based on events generated while reading
through the document.
• Unlike a DOM parser, a SAX parser creates no parse
tree
• It is event-driven, serial access the XML file
elements sequentially.
• This SAX parser reads the XML file from start to
end, calls one method when it encountered one
element, or calls a different method when it found
specific text or attribute.
• SAX is fast and efficient, requires much less
memory than DOM, because SAX does not
create an internal representation (tree
structure) of the XML data, as a DOM does.
• 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.
When to Use?

• You can process the XML document in a linear


fashion from top to down.
• The document is not deeply nested.
SAX Events

• 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.
• uri - the Namespace URI,
• localName - the local name
• qName - the qualified XML name
• void characters(char[] ch, int start, int
length) − Called when character data is
encountered.
• ch - the characters from the XML document
• start - the start position array
• length - the number of characters to read
from the array
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(String qname)
Example
• <name>mkyong</name>
• The SAX parser read the above XML file and
calls the following events or methods
sequentially:
• startDocument()
• startElement() – <name>
• characters() – mkyong
• endElement() – </name>
• endDocument()
Disadvantages of SAX

• We have no random access to an XML


document since it is processed in a forward-
only manner.
• If you need to keep track of data that the
parser has seen or change the order of items,
you must write the code and store the data on
your own.
SAX Parser DOM Parser
1
It is called as Document Object
It is called a Simple API for XML Parsing.
Model.

2
It’s an event-based parser. It stays in a tree structure. 

3
DOM Parser is faster than SAX
SAX Parser is slower than DOM Parser.
Parser. 

4
Best for the larger sizes of files. Best for the smaller size of files.

5
It is read-only. It can insert or delete nodes.

6
In DOM parser backward and
in the SAX parser backward navigation is not possible.
forward search is possible

7
Suitable for efficient memory. Suitable for large XML document.

8
It loads whole XML documents in
A small part of the XML file is only loaded in memory.
memory.
XML Database
• XML database is a data persistence software
system used for storing the huge amount of
information in XML format.
• It provides a secure place to store XML
documents.
• You can query your stored data by using
XQuery, export and serialize into desired
format. XML databases are usually associated
with document-oriented databases.
Types of XML databases

• There are two types of XML databases.


• XML-enabled database
• Native XML database (NXD)
XML-enable Database

• XML-enable database works just like a


relational database.
• It is like an extension provided for the
conversion of XML documents. In this
database, data is stored in table, in the form of
rows and columns.
Native XML Database

• Native XML database is used to store large


amount of data. Instead of table format,
Native XML database is based on container
format. You can query data by XPath
expressions.
• Native XML database is preferred over XML-
enable database because it is highly capable
to store, maintain and query XML documents.

You might also like