|JAXP API using DOM Parser
In the previous issue of Java Jazz Up, you have
read about the JAXP APIs and learned how an
XML document is parsed using the serially
access mode (SAX) parser. Now you will learn
how the DOM parser works with the same xml
document. Lets quickly focus on the overview
of XML parser.
Introduction to XML Parser:
In computing terms, a parser is a program that
takes input in the form of sequential
instructions, tags, or some other defined
sequence of tokens, and breaks them up into
easily manageable parts. XML parser is used to read, update, create and
manipulate an XML document. Whenever the
XML document executes, the parser recognizes
and responds to each XML structure taking
some specified action based on the structure
XML parsers can be validating or nonvalidating.
Validating parser checks the contents of a
document against a set of specific rules i.e. in
what order they must appear. These rules
appear in an XML document either as an optional
XML structure called a document type definition,
or DTD, or as an XML Schema.
Nonvalidating parsers are smaller and faster,
but they do not check documents against the
DTD. They only check whether the XML
document is structurally well formed or not.
Parsing XML Documents
To manipulate an XML document, XML parser is
needed. The parser loads the document into
the computer’s memory. Once the document is
loaded, its data can be manipulated using the
In this section, we will discuss about DOM
parsers of JAXP APIs and for accessing XML
documents in random access mode. The
specifications to ensure the validity of XML
documents are DTDs and the Schemas.
DOM (Document Object Model)
The XML Document Object Model (XML DOM) defines a standard way to access and manipulate XML documents using any programming language (and a parser for that
The DOM presents an XML document as a treestructure
(a node tree), with the elements,
attributes, and text defined as nodes. DOM
provides access to the information stored in
your XML document as a hierarchical object
The DOM converts an XML document into a
collection of objects in an object model in a
tree structure (which can be manipulated in any
way). The textual information in XML document
gets turned into a bunch of tree nodes and a
user can easily traverse through any part of
the object tree, any time. This makes easier to
modify the data, to remove it, or even to insert
a new one. This mechanism is also known as
the random access protocol.
DOM is very useful when the document is small.
DOM reads the entire XML structure and holds
the object tree in memory, so it is much more
CPU and memory intensive. The DOM is most
suited for interactive applications because the
entire object model is present in memory, where
it can be accessed and manipulated by the user.
The Document Object Model implementation is
defined in the following packages:
||Defines the Document
class (a DOM) along with
the classes for all of the
components of a DOM.
||The JAXP APIs provide a
common interface for
different vendors’ to use
SAX and DOM parsers.
The DOM API is defined in org.w3c.dom package
of JAXP-APIs. The DOM API is easier to use. It
provides a tree structure of objects. The DOM
API is used to manipulate the hierarchy of
application objects it encapsulates.
Mar 2008 | Java Jazz Up | 18
|View All Topics
|All Pages of this Issue