indiWiz.com
SiteSearch:
Jump:

Site Map | WizTools.org | jCraze Blog
Web Developer's Den!
Home : WDD : XML
This Article...
Print Version
Add Comment
View Comments
XML
XML - The Bare Facts
XML - Example
Working Of XML Parsers
XHTML - The Bare Facts
Java XML Technologies
Sections

Commniquè
Sign Guestbook!
Read Guestbook!
MailMe!

XML Parsers

XML was developed for enabling high portability of data. XML is structured in such a way that programs called <XML Parsers> can read and display data in multitude formats. Modern web browsers like Mozilla and Internet Explorer have parsing capabilities inbuilt.

XML Parsers may be classified as:

  1. SAX Parser
  2. DOM Parser

SAX Parser

SAX refers to "Simple Application Programming Interface for XML". SAX parsers are also known as event-based parsers.

How SAX Works

SAX parser reads an XML document sequentially and whenever data in the form of entity, text, CDATA are encountered, the corresponding event handler is invoked (thus the name "Event-based parser").

Example

Take the example of this small XML document:

<?xml version="1.0"?>

<favactress>

 <name>Drew Barrymore</name>
 <name>Sharon Stone</name>
 <name>Catherine Zeta Zones</name>

</favactress>

The prototype SAX functions that will parse the above XML is given below (this is just a prototype, this is not based on any one particular language):

function parse_start_tag (tag_name, attributes[]){
	print ("Start Element: " + tag_name);
}

function parse_end_tag (tag_name){
	print ("End Element: " + tag_name);
}

function parse_text (data){
	print ("Data: " + data);
}

Note:

  1. The above functions are called as and when a particular event is encountered.
  2. "attributes[]" parameter in the function parse_start_tag() is an array containing all the attributes for the tag "tag_name".

When the parser having the above prototype is run on the XML document, the result we get is:

Start element: favactress

Start element: name
Data: Drew Barrymore
End element: name

Start element: name
Data: Sharon Stone
End element: name

Start element: name
Data: Catherine Zeta Zones
End element: name

End element: favactress

DOM Parser

The DOM parser creates an internal tree structure of the XML document. This allows the DOM parser to read, add, delete and update XML documents. This is unlike SAX parser, which has only read only access to XML documents. DOM parser also allows random access to data.

<?xml version="1.0"?>
<favactress>

 <actress>
  <name>Drew Barrymore</name>
  <favmovie>Poison Ivy</favmovie>
 </actress>

 <actress>
  <name>Sharon Stone</name>
  <favmovie>Basic Instinct</favmovie>
 </actress>

</favactress>

When a DOM parser parses the above document, it will create a tree structure internally similar to:

favactress
 |
 |---actress
 |    |--name
 |    |   |--Text
 |    |--favmovie
 |    |   |--Text
 |
 |---actress
 |    |--name
 |    |   |--Text
 |    |--favmovie
 |    |   |--Text
 |

As you can see, using the DOM architecture, every aspect of the XML document is available for read, update and write operations.

Comparison: SAX & DOM Parsers

FeatureSAXDOM
AccessSerial & read-only accessRandom & read/write access
TypeEvent drivenTree based
ResourcesAdditional memory beyond application not requiredAdditional memory to internally represent DOM tree needed

User Comments

Add Comment

[Quick Stats: Number of main threads: 0, Number of sub-threads: 0]

Sign Guestbook | Who is Subhash?
The contents of this site are copyright© 2000-2008, indiWiz.com. All Rights Reserved.