XML Parsers: When A Software Program Reads An XML Document and Takes Actions
XML Parsers: When A Software Program Reads An XML Document and Takes Actions
1. DOM Parser
DOM parser load full XML file in memory and creates a tree
representation of XML document.
for small and medium sized XML documents, DOM is much faster
The tree structure for the root element of the document article.xml
This hierarchical tree structure is called a Document Object Model
(DOM) tree, and an XML parser that creates this type of structure is
known as a DOM parser.
In DOM Parser
The DOM tree has a single root node, which contains all the other
nodes in the document.
Each element name (e.g., article, date, firstName) is represented by
a node.
A node that contains other nodes (called child nodes or children) is
called a parent node (e.g., author).
A parent node can have many children, but a child node can have
only one parent node.
Nodes that are peers (e.g., firstName and lastName) are called
sibling nodes.
Uses the XML DOM API to display and manipulate the document’s
element names and values.
The DOM tree contains various nodes like root node, element
node, attribute node, value node etc.
Tree structure allows traversing in top to bottom and vice versa.
The API used by DOM for handling XML document is
Property/Method Description
Node (XML Element)
insertBefore Inserts the node (passed as the first argument) before the
existing node (passed as the second argument). If the new
node is already in the tree, it’s removed before insertion
NodeList
Method that receives an index number and returns the
iItem(index i)
element node at that index. Indices range from 0 to length
– 1.
length() The total number of nodes in the list.
createElement Creates and returns an element node with the specified tag
name.
createAttribute Creates and returns an Attr node with the specified name
and value.
Attribute
value The specified attribute’s value.
2. SAX Parser
In SAX Parser
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.
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, we must write the code and store the data on your
own.
Load entire document in memory as DOM Does not load entire document.
Tree
Effectively suitable for smaller and efficient SAX is suitable for large XML files
memory, not for large XML files.
DOM provides API for traversing the tree in SAX provides API for traversing only in top
top to bottom and vice versa and in to bottom.
random access.
DOM allows to perform read, write and SAX does allow only read operation,
update XML document. cannot update directly.
The API for DOM is provides by the XML The API for SAX is provided by the
Parser. application languages like javascript, php,
java.