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().
Below is a code snippet that 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(" - Edition: ");
document.write(z[i].childNodes[0].nodeValue);
document.write("<br />");
}
ఈ కోడ్ యొక్క అవుట్పుట్:
Everyday Italian - Edition: First Harry Potter - Edition: First XQuery Kick Start - Edition: First Learning XML - Edition: First
గమనిక:Internet Explorer యొక్క నోడ్ల మధ్య అందుబాటులో ఉండే శూన్య టెక్స్ట్ నోడ్లను మించివుంటుంది (ఉదాహరణకు రాకలు అక్షరాలు), కానీ Mozilla అలా చేయదు. అందువల్ల, మేము మాత్రమే ఎలిమెంట్ నోడ్లను (ఎలిమెంట్ నోడ్ల యొక్క nodeType ఇది 1) నిర్వహిస్తాము.
IE మరియు Mozilla బ్రౌజర్ల మధ్య వ్యత్యాసాలగురించి మరింత సమాచారం తెలుసుకోవడానికి మీరు CodeW3C.com యొక్క XML DOM ట్యూటోరియల్ ను సందర్శించండి DOM బ్రౌజర్ ఈ భాగం.