32
Signure Technology Pvt Ltd. www.signure.com Seminar for DOM – Document Object Model

DOM Quick Overview

Embed Size (px)

DESCRIPTION

This is a seminar presentation

Citation preview

Page 1: DOM Quick Overview

Signure Technology Pvt Ltd.www.signure.com

Seminar for DOM – Document Object Model

Page 2: DOM Quick Overview

HTML DOM

• The HTML DOM defines a standard way for accessing and manipulating HTML documents.

• The HTML DOM is platform and language independent and can be used by any programming language like Java, JavaScript, and VBScript.

Page 3: DOM Quick Overview

Tree-Structure (a node tree), with elements, attributes, and text.

Every HTML tag is a node within this tree , with subnodes & parent nodes. Also every text portion is its own DOM node.

Page 4: DOM Quick Overview

HTML DOM Introduction

What is the DOM?"The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document."

The DOM defines the objects and properties of all document elements, and the methods (interface) to access them.

Page 5: DOM Quick Overview

DOM

The DOM is separated into 3 different parts / levels:

Core DOM - standard model for any structured document

XML DOM - standard model for XML documents HTML DOM - standard model for HTML documents

Warning: - The DOM is accessible only when the whole document has been loaded.That’s the reason the DOM access code is executed only after the load event has been fired.

Page 6: DOM Quick Overview

Features of DOM

A standard object model for HTML A standard programming interface for HTML Platform- and language-independent A W3C standard.

The HTML DOM is a standard for how to get, change, add, or delete HTML elements.

The HTML DOM defines the objects and properties of all HTML elements, and the methods (interface) to access them.

Page 7: DOM Quick Overview

HTML DOM Nodes

The entire document is a document node Every HTML tag is an element node The text in the HTML elements are text nodes Every HTML attribute is an attribute node Comments are comment nodes

According to the DOM, everything in an HTML document is a node.

Page 8: DOM Quick Overview

SAX Classes and Interfaces

• Parser related classes :-• javax.xml.parsers

-javax.xml.parsers.SAXParserFactory-javax.xml.parsers.SAXParser

• Parser related Exception classes :-• ParserConfigurationException

A ParserConfigurationException signals that a factory is unable to load and instantiate a parser class.

• FactoryConfigurationError• A FactoryConfigurationError signals that Java is

unable to load and instantiate the concrete factory class.

Page 9: DOM Quick Overview

HTML DOM Node Tree (Document Tree)

All the nodes in a node tree have relationships to each other.

The tree structure is called a node-tree.

Node:- Parents, Children, and Siblings

Page 10: DOM Quick Overview

HTML DOM Node Tree (Document Tree)

• In a node tree, the top node is called the root• Every node, except the root, has exactly one parent node• A node can have any number of children• A leaf is a node with no children• Siblings are nodes with the same parent

Page 11: DOM Quick Overview

DOM

<html><head>

<title>DOM</title> </head>

<body> <h1>I am in DOM world</h1> <p id=“intro”>Hello DOM!</p>

</body> </html>

• The <html> node has no parent node; the root node• The parent node of the <head> and <body> nodes is the <html>

node• The parent node of the "Hello world!" text node is the <p> node

Page 12: DOM Quick Overview

DOM

• Most element nodes have child nodes:• The <html> node has two child nodes; <head> and <body>• The <head> node has one child node; the <title> node• The <title> node also has one child node; the text node

"DOM Tutorial"• The <h1> and <p> nodes are siblings, and both child nodes

of <body>

Page 13: DOM Quick Overview

HTML DOM Properties and Methods

• Properties are often referred to as something that is (i.e. nodename is "p").

• Methods are often referred to as something that is done (i.e. delete "p").

• XML DOM Properties These are some typical DOM properties: x.nodeName - the name of x x.nodeValue - the value of x x.parentNode - the parent node of x x.childNodes - the child nodes of x x.attributes - the attributes nodes of x

• Note: In the list above, x is a node object.

Page 14: DOM Quick Overview

XML DOM Methods

• x.getElementByID(id) - get the element with a specified id• x.getElementsByTagName(name) - get all elements with a

specified tag name• x.appendChild(node) - insert a child node to x• x.removeChild(node) - remove a child node from x

• Note: In the list above, x is a node object.

Page 15: DOM Quick Overview

Example

The JavaScript code to get the text from a <p> element with the id "intro" in a HTML document:

txt=document.getElementById("intro").childNodes[0].nodeValue

Display:- “Hello DOM!”

• document - the current HTML document• getElementsById("intro") - the <p> element with the id "intro"• childNodes[0] - the first child of the <p> element (the text node)• nodeValue - the value of the node (the text itself)

getElementsById is a method, while childNodes and nodeValue are properties.

Page 16: DOM Quick Overview

HTML DOM Access Nodes

Accessing Nodes

You can access a node in three ways: 1. By using the getElementById() method 2. By using the getElementsByTagName() method 3. By navigating the node tree, using the node

relationships )

Page 17: DOM Quick Overview

The getElementById() Method

getElementById(); method returns the element with the specified ID

Syntaxnode.getElementById("someID");

Exampledocument.getElementsById(“intro");

Note: The getElementById() method doesn't work in XML.

Page 18: DOM Quick Overview

The getElementsByTagName() Method

getElementsByTagName(); returns all elements with a specified tag name.

Syntaxnode.getElementsByTagName("tagname");

Example document.getElementsByTagName("p");

Page 19: DOM Quick Overview

Example – getElementsByTagName()

• <html>• <body>• <p id="intro">example</p>• <div id="main">

<p id="main1"><font>The DOM is very useful</font></p> <p id="main2">This example demonstrates how to use the

<b>getElementsByTagName</b> method</p>• </div>• <script type="text/javascript">• x=document.getElementsByTagName("p");• document.write("First paragraph text: " +

x[0].childNodes[0].nodeValue);• </script>• </body>

Page 20: DOM Quick Overview

getElementsByTagName()

example returns a nodeList of all <p> elements that are descendants of the element with id="main":

document.getElementById('main').getElementsByTagName("p");

Page 21: DOM Quick Overview

getElementsByTagName()

<html><body><p id="intro">W3Schools example</p><div id="main"><p id="main1">The DOM is very useful</p><p id="main2">This example demonstrates how to use the <b>getElementsByTagName</b> method</p></div><script type="text/javascript">x=document.getElementById("main").getElementsByTagName("p");document.write("First paragraph inside the main div: " + x[0].childNodes[0].nodeValue);</script></body></html>

Page 22: DOM Quick Overview

DOM Node List Length

x=document.getElementsByTagName("p");for(i=0;i<x.length;i++){

document.write(x[i].childNodes[0].nodeValue); document.write("<br />"); }

Page 23: DOM Quick Overview

Navigating Node Relationships

• The three properties parentNode, firstChild, and lastChild follow the document structure and allow short-distance travel in the document.

<html><body>

<p id="intro">W3Schools example</p><div id="main">

<p id="main1">The DOM is very useful</p><p id="main2">This example demonstrates <b>node Relationships</b></p>

</div></body></html>

Page 24: DOM Quick Overview

Navigating Node Relationships

• the <p id="intro"> is the first child node (firstChild) of the <body> element, and the <div> element is the last child node (lastChild) of the <body> element.

• Furthermore, the <body> is the parent (parentNode) .

• The firstChild property can be used to access the text of an element.

• x=document.getElementById("intro");var text=x.firstChild.nodeValue;

Page 25: DOM Quick Overview

Root Nodes

There are two special document properties that allow access to the tags:

•document.documentElement•document.body

The first property returns the root node of the document and exists in all XML and HTML documents.

The second property is a special addition for HTML pages, and gives direct access to the <body> tag.

Page 26: DOM Quick Overview

Node Property

Everything in a document can be considered a node, including the document itself.

Properties Of Node Interface:• nodeName:Returns name of element.

• nodeValue:Returns value of element.

•nodeType:Returns type of node whether element or document etc.

• parentNode:Contains the parent node (for nodes that can have parents).

•childNodes:Contains a node list containing the children (for nodes that can have children).

• firstChild:Contains the first child of this node.

Page 27: DOM Quick Overview

Node Property

Properties Of Node Interface:

• lastChild: Returns the last child node.

• previousSibling: Contains the left sibling of this node

• nextSibling:Contains the next sibling of this node in the parent's child list.

• attributes:Contains the list of attributes for this node.

getAttributes();setAttributes();

Page 28: DOM Quick Overview

Node Property

Methods Of Node Interface:

• insertBefore(newchild,refchild):Inserts a child node to the left of the specified node or at the end of the list.

• replaceChild(newchild,oldchild):Replaces the specified old child node with the supplied new child node in the set of children of this node, and returns the old child node.

• removeChild(oldchild):Removes the specified child node from the list of children and returns it.

Page 29: DOM Quick Overview

Node Property

appendChild(newchild):appendChild(newchild):Appends newChild as the last child Appends newChild as the last child of this nodeof this node

createTextNode = This property used to create new text node.createTextNode = This property used to create new text node.e.g. createTextNode(a);e.g. createTextNode(a);createElement = This property usedt to create element createElement = This property usedt to create element e.g. createElement('tr');e.g. createElement('tr');

Page 30: DOM Quick Overview

Node List

The nodeList object represents a node and its child nodes as a The nodeList object represents a node and its child nodes as a node tree. node tree. Property of Nodelist:Property of Nodelist:length:length:Returns an unsigned long integer indicating the number Returns an unsigned long integer indicating the number of nodes in the NodeListof nodes in the NodeList..Method of Nodelist:Method of Nodelist:item(index):item(index):This method takes an index as its argument and This method takes an index as its argument and returns the node at that index position.returns the node at that index position.

Page 31: DOM Quick Overview

Properties of Document:

doctype: doctype: Returns <!DOCTYPE information of document.Returns <!DOCTYPE information of document.implementation: implementation: Returns implementation node.Returns implementation node.documentElement: documentElement: Returns root element of the document.Returns root element of the document.

Page 32: DOM Quick Overview

Methods of DOM

Methods Of Document Interface:

• createElement(tagName) :Creates an new

element with the specified tagName

• createTextNode(text) :Creates a text node, containing the specified text .

• createAttribute(attributeName) :Creates an attribute node with the specified attribute name