WINSEM2012-13 CP0557 03-Jan-2013 RM02 Dom
WINSEM2012-13 CP0557 03-Jan-2013 RM02 Dom
The DOM is a W3C (World Wide Web Consortium) standard. The DOM defines a standard for accessing HTML and XML documents: "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 W3C DOM standard is separated into 3 different parts:
What is the XML DOM? The XML DOM defines the objects and properties of all XML elements, and the methods to access them. What is the HTML DOM? The HTML DOM is: A standard object model for HTML A standard programming interface for HTML A W3C standard The HTML DOM defines the objects and properties of all HTML elements, and the methods to access them. In other words:
With the HTML DOM, all nodes in the tree can be accessed by JavaScript. All HTML elements (nodes) can be modified, and nodes can be
The following image illustrates a part of the node tree and the relationship between the nodes:
<html> <head> <title>DOM utorial</title> </head> <body> <h1>DOM Lesson1</h1> <p>Hello world!</p> </body> </html>
HTML DOM Methods Methods are actions you can perform on nodes (HTML Elements) Programming Interface The HTML DOM can be accessed with JavaScript (and other programming languages). All HTML elements are defined as objects, and the programming interface is the object methods and object properties . A method is an action you can do (like add or modify an element). A property is a value that you can get or set (like the name or content of a node). The getElementById() Method The getElementById() method returns the element with the specified ID: Example var element=document.getElementById("intro");
Example
The following code gets the innerHTML from the <p> element with id="intro": Example <html> <body> <p id="intro">Hello World!</p> <script> var txt=document.getElementById("intro").innerHTML; document.write(txt); </script> </body> </html>
No 5 No
1 1 1
9 9 9
isElementContentWhitespace
No No No Yes
length
Yes
wholeText
Returns all text of text nodes No No No Yes adjacent to this node, concatenated in document order
Replaces the text of this node and No No No Yes all adjacent text nodes with the specified text Splits this node into two nodes at the specified offset, and returns the new node that contains the text after the offset Extracts data from the node 6 1 9 Yes
splitText()
substringData()
Yes
CDATASection Object Properties Property data length Description Sets or returns the text of this node Returns the length of the CDATA section IE F O W3 C 6 1 N Ye o s 6 1 N Ye o s
Example
xmlDoc=loadXMLDoc("books.xml"); //the x variable will hold a node list x=xmlDoc.getElementsByTagName('title'); for (i=0;i<x.length;i++) { document.write(x[i].childNodes[0].nodeValue); document.write(" "); } Output: Everyday Italian Harry Potter XQuery Kick Start Learning XML