Académique Documents
Professionnel Documents
Culture Documents
DOCUMENT OBJECT
MODEL (DOM)
What is DOM?
The XML Document Object Model (DOM) is:
A standard for accessing and manipulating XML document
A standard programming interface for XML, eg. javascript
Platform and language independent
A W3C standard
xmlDoc=loadXMLDoc("sample.xml");
x=xmlDoc.getElementsByTagName("head")[0].childNodes;
document.write(x.nodeValue);
xmlDoc=loadXMLDoc("sample.xml");
x=xmlDoc.documentElement.childNodes;
for(i=0;i<x.length;i++)
{
if(x[i].nodeType==1){//Processonlyelementnodes(type1)
document.write(x[i].nodeName);
document.write("");
}
}
x=xmlDoc.getElementsByTagName("head")[0].childNodes;
y=xmlDoc.getElementsByTagName("head")[0].firstChild;
for(i=0;i<x.length;i++)
{
if(y.nodeType==1){//Processonlyelementnodes(type1)
document.write(y.nodeName+"");
}
y=y.nextSibling;
}
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
Everyday Italian
Getting the value of a
node
Even if the HTML document contains only ONE <h1> element you still have to
specify the array index [0], because the getElementsByTagName() method
always returns an array.
Getting an Attribute Node
The getAttributeNode() method returns
an attribute node.
The following code retrieves attribute
node of the "lang" attribute of the first
<title> element:
x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");
document.write(x)
[object Attr]
Getting the Value of an Attribute
After action