XML DOM appendChild() method
Definition and Usage
The appendChild() method adds a node after the last child node of the specified element node.
This method returns the new child node.
Syntax:
appendChild(node)
Parameter | Description |
---|---|
node | Required. The node to be appended. |
Example
In all examples, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
The following code snippet creates and appends a node to the first <book> element and then outputs all child nodes of the first <book> element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
var newel,newtext;
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement('edition');
newtext=xmlDoc.createTextNode('First');
newel.appendChild(newtext);
x[i].appendChild(newel);
}
//Output all titles and editions
y=xmlDoc.getElementsByTagName("title");
z=xmlDoc.getElementsByTagName("edition");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write(" - संस्करण: ");
document.write(z[i].childNodes[0].nodeValue);
document.write("<br />");
}
इस कोड का आउटपुट:
Everyday Italian - संस्करण: पहला Harry Potter - संस्करण: पहला XQuery Kick Start - संस्करण: पहला Learning XML - संस्करण: पहला
टिप्पणी:Internet Explorer नोड के बीच बने खाली टेक्स्ट नोड को नज़रअंदाज़ करेगा (उदाहरण के लिए, बदले पट्टी कारक), जबकि Mozilla ऐसा नहीं करता।इसलिए, उपरोक्त उदाहरण में हमें केवल एलीमेंट नोड को ही संसाधित किया जाता है (एलीमेंट नोड का nodeType 1 है)。
IE और Mozilla ब्राउज़र के बीच के अंतर के बारे में अधिक जानकारी के लिए, CodeW3C.com के XML DOM शिक्षा में देखें DOM ब्राउज़र इस खंड का.