Final 1
Final 1
M Accessing Nodes XML DOM Node Information DOM Node List Manipulate Nodes
Introduction of DOM
The DOM is a W3C (World Wide Web Consortium) standard.
The DOM defines a standard for accessing documents like XML
and HTML 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 The DOM defines the objects and properties of all document elements, and the methods (interface) to access them.
XML DOM
A standard object model for XML
A standard programming interface for XML Platform- and language-independent The XML DOM defines the objects and properties of all
XML elements, and the methods (interface) to access them. In other words: The XML DOM is a standard for how to get, change, add, or delete XML elements.
DOM Nodes
According to the DOM, everything in an XML document is a node.
The DOM says: The entire document is a document node Every XML element is an element node The text in the XML elements are text nodes Every attribute is an attribute node Comments are comment nodes
EXAMPLE
<bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Root node - <book store> .All other nodes in the document are contained within this tag. It holds the four <book > nodes. The first <book> node holds four nodes <title>,<author>,<year>,<price>.
XML Parser
What is Parser? Parser is a program, usually part of compiler, that receives input in the form of sequential source program instruction or markup tags or some other defined interface and breaks them up into parts that can be managed by other programming.
The XML DOM contains methods (functions) to traverse
XML trees, access, insert, and delete nodes. However, before an XML document can be accessed and manipulated, it must be loaded into an XML DOM object. An XML parser reads XML, and converts it into an XML DOM object that can be accessed with JavaScript. Most browsers have a built-in XML parser.
Creating a DOMParser:
To create a DOMParser object simply use new DOMParser().
Parsing XML: Once we have created a parser object, you can parse XML from a string using the parseFromString method:
Example: var parser = new DOMParser(); var doc = parser.parseFromString(stringContainingXMLSource, "application/xml");
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
specified tag name x.appendChild(node) - insert a child node to x x.removeChild(node) - remove a child node from x
Example: xmlDoc - the XML DOM object created by the parser. getElementsByTagName("title")[0] - the first <title> element childNodes[0] - the first child of the <title> element (the text node) nodeValue - the value of the node (the text itself)
Accessing Nodes
You can access a node in three ways:
By using the getElementsByTagName() method
relationships.
getElementsByTagName(), a node list object is returned. A node list object represents a list of nodes, in the same order as in the XML. Nodes in the node list are accessed with index numbers starting from 0.
Manipulating Nodes
DOM Get values:
The nodeValue property is used to get the text value of a node. The getAttribute() method returns the value of an attribute. The nodeValue property is used to change a node value. The setAttribute() method is used to change an attribute value. The removeChild() method removes a specified node. The removeAttribute() method removes a specified attribute. The replaceChild() method replaces a specified node. The nodeValue property replaces text in a text node.
DOM Create nodes: CreateElement() to create a new element node createAttribute() to create a new attribute node createTextNode() to create a new text node
by
Geetha Kalaivani Kavitha