Before we discuss exactly what the DOM is,we should know what led to its creation.Although the DOM was heavily influenced by the rise of Dynamic HTML in browsers,the W3C took a step backward and first applied it to XML.
Introduction to XML
The eXtensible Markup Language (XML) was derived from an earlier language called Standard Generalized Markup Language(SGML). SGML’s main purpose was to define the syntax of markup languages to represent data using tags.
Tags consist of text enclosed between a less-than symbol(<) and a greater-than symbol(>), as in <tag>.Start tags begin a particular area,such as <start>;end tags define the end of an area.They look the same as start tags but have a forward slash (/) immediately following the less-than symbol,as in </end>.SGML also defines attributes for tags, which are values assigned inside of the less-than and greater-than symbols, such as the src attribute in <img src=”picture.jpg”>.If this looks familiar,it should; the most famous implementation of an SGML-based language is the original HTML.
SGML was used to define the Document Type Definition(DTD) for HTML, and it is still used to write DTDs for XML.The problem with SGML is its allowances for odd syntax, which makes creating parsers for HTML a difficult problem:
All these issues make creating SGML language parsers a truly arduous task.The difficultly of knowing when to apply the rules caused a stagnation in the definition of SGML languages.This is where XML begins to fit in.XML does away with all the optional syntax of SGML that caused so many developers heartache early on.In XML, the following rules apply:
These rules make an XML parser much simpler to develop and also remove the guesswork of when and where to apply odd syntax rules.Where SGML failed to gain mainstream acceptance,XML has made tremendous inroads because of its simplicity. XML has spawned several languages in just the first six years of its existence, including MathML, SVG, RDF, RSS, SOAP, XSLT, XSL-FO, and the reformulation of HTML into XHTML.
Today XML is one of the fastest-growing technologies in the world.Its main purpose is to represent data in a structured way using plain text.In some ways, XML files are not unlike databases,which also represent a structured view of data.Here is an example XML file:
Beginning XML, 3rd Edition, like the first two editions, begins with a broad overview of the technology and then focuses on specific facets of the various specifications for the reader. This book teaches you all you need to know about XML:what it is, how it works, what technologies surround it,and how it can best be used in a variety of situations, from simple data transfer to using XML in your Web pages.It builds on the strengths of the first and second editions, and provides new material to reflect the changes in the XML landscape - notably RSS and SVG.
If you’re a Java programmer working with XML, you probably already use some of the tools developed by the Apache Software Foundation.This book is a code-intensive guide to the Apache XML tools that are most relevant for Java developers,including Xerces, Xalan, FOP, Cocoon, Axis,and Xindice.
Every XML document begins with the XML prolog, which is the first line in the previous code, <?xml version=”1.0”?>.This line alone tells parsers and browsers that this file should be parsed based on the XML rules discussed earlier. The second line, <books>,is the document element,which is the outermost start tag in the file (an element is considered the contents of a start tag and end tag).All other tags must be contained within this one in order to constitute a valid XML file. The second line of the XML file need not always contain the document element; it can come later if comments or other (???)
The third line in this sample file is a comment, which you may recognize as the same style comment used in HTML. This is one of the syntax elements XML inherited from SGML.
Alittle bit farther down the page you find a <desc> tag with some special syntax inside it.The <![CDATA[ ]]> code is used to indicate text that should not be parsed,allowing special characters such as less-than and greater-than to be included without fear of breaking the XML syntax. The text must appear between <![CDATA[ and ]]> to be properly shielded from parsing. This is called a Character Data Section or CData Section for short.
The following line is just before the second book definition:<?page render multiple authors ?>
Even though this looks like the XML prolog, it is actually considered a different type of syntax called a processing instruction. The purpose of processing instructions (or PIs for short) is to provide extra information to programs that are processing the page, such as XML parsers. PIs are generally free form. Their only requirement is that a letter must follow the first question mark. After that point, a PI can contain any sequence of characters aside from the less-than or greater-than symbols.
The most common PI is used to specify a style sheet for an XML file:<?xml-stylesheet type=”text/css”” href=”MyStyles.css” ?>
This PI is typically placed immediately after the XML prolog and is used by Web browsers to display the XML data using particular styles.
An API for XML
After XML was defined as a language, the need arose for a way to both represent and manipulate XML code using common programming languages such as Java.
First came the Simple API for XML (SAX) project for Java. SAX provides an event-based API to parse XML. Essentially, SAX parsers start out at the beginning of the file and parse their way through the code in one straight pass, firing events every time it encounters a start tag, end tag, attribute, text, or other XML syntax. It is up to the developer, then, to determine what to do when each of these events occurs.
SAX parsers are lightweight and fast because they just parse the text and continue on their way. Their main downside is the inability to stop, go backward, or access a specific part of the XML structure without starting from the beginning of the file.
The Document Object Model (DOM) is a tree-based API for XML.Its main focus isn’t just to parse XML code, but rather to represent that code using a series of interlinked objects that can be modified and accessed directly without reparsing the code.
Hierarchy of nodes
So what exactly is a tree-based API? When talking about DOM trees (which are called documents),you are really talking about a hierarchy of nodes. The DOM defines the Node interface as well as a large number of node types to represent the multiple aspects of XML code:
A document is made up of a hierarchy of any number of these nodes. Consider the following XML code:
This code can be represented in a DOM document as displayed each rectangle represents a node in the DOM document tree, with the bold text indicating the node type and the nonbold text indicating the content of that node.
Both the comment and <employee/> nodes are considered to be child nodes of <employees/> because they fall immediately underneath it in the tree.Likewise, <employees/> is considered the parent node of the comment and <employee/> nodes.
Similarly,<name/>, <position/>,and <comments/> are all considered child nodes of <employee/> and are also considered siblings of each other because they exist at the same level of the DOM tree and have the same parent node.
The <employees/> node is considered the ancestor of all nodes in this section of the tree, including its children (the comment and <employee/>) as well as their children (<name/>, <position/>, and so on, all the way down to the text node “His birthday is on 8/14/68”). The document node is considered the ancestor of all nodes in the document. The Node interface defines 12 constants that map to the different node types
The Node interface also defines a set of properties and methods that all node types contain. These properties and methods are listed out in the following table:
In addition to nodes,the DOM also defines some helper objects, which are used to work with nodes but are not necessarily part of a DOM document:
These helper objects provide additional access and traversal methods for dealing with DOM document.
Any XML-based language,such as XHTML and SVG, can make use of the core DOM just introduced because they are technically XML.However,many languages go on to define their own DOMs that extend the XML core to provide language-specific features.
Along with developing the XML DOM,the W3C concurrently developed a DOM more specific to XHTML (and HTML). This DOM defines an HTMLDocument and HTMLElement as the basis for the implementation.Each HTML element is represented by its own HTMLElement type,such as HTMLDivElement representing <div>,with the exception of a small subset of elements that don’t require special properties or methods other than those provided by HTML Element.
Java Script Related Tutorials
|Adv Java Tutorial||Core Java Tutorial|
|Java-Springs Tutorial||Java Servlets Tutorial|
|EJB(Enterprise JavaBeans) Tutorial||JavaServer Faces (JSF) Tutorial|
|Java Swing Tutorial||Java Tutorial|
|JavaMail API Tutorial||Java 8 Tutorial|
|Java XML Tutorial||Java Bean Utils Tutorial|
|The Java Debugger (JDB) Tutorial||Java.math Package Tutorial|
Java Script Related Interview Questions
|Java Script Interview Questions||Adv Java Interview Questions|
|Core Java Interview Questions||Java-Springs Interview Questions|
|Java Servlets Interview Questions||EJB(Enterprise JavaBeans) Interview Questions|
|JavaServer Faces (JSF) Interview Questions||Java Swing Interview Questions|
|Java 8 Interview Questions||Java XML Interview Questions|
|JavaFX Interview Questions||The Java Debugger (JDB) Interview Questions|
Java Script Related Practice Tests
|Java Script Practice Tests||Adv Java Practice Tests|
|Core Java Practice Tests||Java-Springs Practice Tests|
|Java Servlets Practice Tests||EJB(Enterprise JavaBeans) Practice Tests|
|JavaServer Faces (JSF) Practice Tests||Java 8 Practice Tests|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.