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

XML Processors

The document discusses XML parsing, focusing on two main types of XML processors: DOM (Document Object Model) and SAX (Simple API for XML). DOM provides a tree-based interface for accessing and manipulating XML documents, while SAX offers an event-driven approach that is faster and uses less memory. The document also highlights the advantages and differences between DOM and SAX, along with an example of using a DOM parser in JavaScript.

Uploaded by

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

XML Processors

The document discusses XML parsing, focusing on two main types of XML processors: DOM (Document Object Model) and SAX (Simple API for XML). DOM provides a tree-based interface for accessing and manipulating XML documents, while SAX offers an event-driven approach that is faster and uses less memory. The document also highlights the advantages and differences between DOM and SAX, along with an example of using a DOM parser in JavaScript.

Uploaded by

dyagalavarshith
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

XML PROCESSORS-DOM AND SAX

XML parsing is the process of reading an XML document and


providing an interface to the user application for accessing the
document. An XML parser is a software apparatus that accomplishes
such tasks. In addition, most XML parsers check the well-formedness
of the XML document and many can also validate the document with
respect to a DTD (Document Type Definition) or XML schema.
HISTORICAL BACKGROUND:
DOM (Document Object Model) was initially used for modelling
HTML (Hypertext Markup Language) by various Web browsers. As
inconsistencies existed among the individual DOM models adopted
by different browsers, interoperability problems arose in developing
browser-neutral HTML codes. W3C (World Wide Web Consortium)
standardized and released DOM Level 1 specification on October 1,
1998, with support for both HTML and XML. DOM Level 2 was
released in November 2000 and added namespace support. The latest
specification DOM Level 3 was released in April 2004.
SAX (Simple API for XML) was developed in late 1997 through the
collaboration of several implementers of early XML parsers and many
other members of the XML-DEV mailing list. The goal was to create
a parser independent interface so that XML applications can be
developed without being tied to the proprietary API (Application
Programming Interface) of any specific parser. SAX1 was finalized
and released on May 11, 1998. The latest release SAX2, finalized in
May 2000, includes namespace support.
XML Programming Interfaces Most XML parsers can be classified
into two broad categories, based on the types of API that they provide
to the user applications for processing XML documents.
DOM(Document Object Model)
DOM is a tree-based interface that models an XML document as a
tree of various nodes such as elements, attributes, texts, comments,
entities, and so on. A DOM parser maps an XML document into such
a tree rooted at a Document node, upon which the application can
search for nodes, read their information, and update the contents of
the nodes.
A DOM tree is made up of Nodes. Most frequently used types of
Nodes:
Document: root of the DOM tree. Not the same as the root element of
XML
DocumentType: corresponds to the DOCTYPE declaration in an
XML document
Element: corresponds to an XML element
Attr: corresponds to an attribute of an XML element
Text: corresponds to chunk of text
SAX(Simple API for XML)
SAX is an event-driven interface. The application receives document
information from the parser through a ContentHandler object. It
implements various event handlers in the interface methods in
ContentHandler, and registers the ContentHandler object with the
SAX parser. The parser reads an XML document from the beginning
to the end. When it encounters a node in the document, it generates an
event that triggers the corresponding event handler for that node. The
handler thus applies the application logic to process the node
specifically.
SAX events Most frequently used events:
startDocument: Receive notification of the beginning of a document.
endDocument: Receive notification of the end of a document.
startElement: Receive notification of the beginning of an element.
endElement: Receive notification of the end of an element.
Characters: Receive notification of character data.
DIFFERENCES BETWEEN SAX AND DOM
SAX: Because of one-pass processing, a SAX parser is fast, consumes
very little memory, and scales to very large XML documents.
Applications are responsible for keeping necessary state in memory,
and are therefore more difficult to code.
DOM: Because the input XML needs to be converted to an in-
memory DOM-tree representation, a DOM parser consumes more
memory, and does not scale as well as a SAX parser. Applications are
easier to develop because of the powerful DOM interface.
Advantages of using DOM
1. Any part of the document can be accessed more than once by the
application.
2. Rearrangement of the elements in the document can be done.
3. Accesses to random parts of the document are possible
4. Avoids processing of the document that is later found to be invalid
Advantages of SAX
1. DOM structure produces large documents and they require a great
deal of memeory. As there is no limit in size of XML, some
documents cannot be parsed using DOM.
2. SAX method is faster than DOM approach.

EXAMPLE:
<html>
<body>

<p id="demo"></p>

<script>
var text, parser, xmlDoc;

text = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";

parser = new DOMParser();


xmlDoc = parser.parseFromString(text,"text/xml");

document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")
[0].childNodes[0].nodeValue;
</script>

</body>
</html>
EXPLANATION OF EXAMPLE:
A TEXT STRING IS DEFINED
text = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
AN XML DOM PARSER IS CREATED
parser = new DOMParser();
The parser creates a new XML DOM object using the text string:
xmlDoc = parser.parseFromString(text,"text/xml");

You might also like