Chapter-6 XML DOM
Chapter-6 XML DOM
Chapter-6 XML DOM
X M L D O C U MEN T O B J E CT M O D EL ( D O M )
A W3C standard
This code retrieves the text value of the first <title> element in an XML
document:
E.G.
txt= xmlDoc.getElementsByTagName(“title”)[0].childNodes[0].nodeValue;
XMLHttpRequest object:
xmlhttp.open("GET","xmlhttp_info.txt",true);
xmlhttp.send();
Method Description
open(method,url,async) Specifies the type of request, the URL, and if the request should be
handled asynchronously or not.
SynchronousFalse
◦ Sending asynchronously requests is better,
By sending asynchronously, the JavaScript does not have to wait for the
server response, but can instead:
◦ execute other scripts while waiting for server response
Property Description
onreadystatechang Stores a function (or the name of a function) to be called automatically each time
e the readyState property changes
The terms parent, child, and sibling are used to describe the
relationships. Parent nodes have children. Children on the
same level are called siblings (brothers or sisters).
nextSibling, previousSibling
etc
getElementsByTagName() Method
◦ Syntax:
The <title> elements in x can be accessed by index number. To access the third <title>
you can write::
◦ y = x[2];
txt = "";
x = xmlDoc.documentElement.childNodes;
◦ nodeName is read-only
The following code changes the text node value of the first
<title> element
var x = xmlDoc.getElementsByTagName("title")
[0].childNodes[0];
x.nodeValue = "Easy Cooking";
This is called a named node map, and is similar to a node list, except
for some differences in methods and properties.
This code fragment returns a list of attribute nodes from the first
<book> element in "books.xml":
DIOT, INTEGRATIVE PROGRAMMING AND TECHNOLOGIES, XML
02/16/2023
DOM
35
Cont’d …
x = xmlDoc.getElementsByTagName('book')
[0].attributes;
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
The way to change the value of an attribute, is to change its text value.
◦ When a node is removed, all its child nodes are also removed.
Example:
◦ xmlDoc=loadXMLDoc("books.xml");
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
Example
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);
DIOT, INTEGRATIVE PROGRAMMING AND TECHNOLOGIES, XML
02/16/2023
DOM
46
Remove a Text Node
The removeChild() method can also be used to remove a
text node:
Example
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);
Example
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";
Example: removeAttribute('category')
Example
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
Example
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement;
newNode=xmlDoc.createElement("book");
newTitle=xmlDoc.createElement("title");
newText=xmlDoc.createTextNode("A Notebook");
starts at zero
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")
[0].childNodes[0];
x.replaceData(0,8,"Easy");
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Italian";
Example
◦ xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
Example
◦ xmlDoc=loadXMLDoc("books.xml");
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);
DIOT, INTEGRATIVE PROGRAMMING AND TECHNOLOGIES, XML
02/16/2023
DOM
56
Create an Attribute Using setAttribute()
Since the setAttribute() method creates a new attribute if the
attribute does not exist, it can be used to create a new attribute.
Example
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
DIOT, INTEGRATIVE PROGRAMMING AND TECHNOLOGIES, XML
02/16/2023
DOM
58
XML DOM Add Nodes
Add a Node - appendChild()
◦ The appendChild() method adds a child node to an existing node.
◦ The new node is added (appended) after any existing child nodes.
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
This method is useful when the position of the added node is important:
Example
◦ xmlDoc=loadXMLDoc("books.xml");
newNode=xmlDoc.createElement("book");
x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book")[3];
x.insertBefore(newNode,y);
DIOT, INTEGRATIVE PROGRAMMING AND TECHNOLOGIES, XML
02/16/2023
DOM
61
Add a New Attribute
There is no method called addAtribute().
Example
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");
DIOT, INTEGRATIVE PROGRAMMING AND TECHNOLOGIES, XML
02/16/2023
DOM
62
Add Text to a Text Node - insertData()
The insertData() method inserts data into an existing text node.
Example
◦ xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.insertData(0,"Easy ");
The following code fragment copies the first <book> node and
appends it to the root node of the document:
oldNode=xmlDoc.getElementsByTagName('book')[0];
newNode=oldNode.cloneNode(true);
xmlDoc.documentElement.appendChild(newNode);
QUESTIONS?