DOM XML appendChild()Méthodes
Objet Element
Définition et utilisation
La méthode appendChild() ajoute un nœud après le dernier nœud enfant du nœud d'élément spécifié.
Cette méthode renvoie le nouveau nœud enfant.
Syntaxe
appendChild(node)
| Paramètre | Description |
|---|---|
| nœud | Requis. Le nœud à ajouter. |
Exemple 1
L'extrait de code suivant utiliseloadXMLDoc()pour charger "" dans xmlDoc, crée un nœud (<edition>), et l'ajoute après le dernier nœud enfant du premier élément <book> :
Exemple
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
document.write(x.getElementsByTagName("edition")[0].nodeName);
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
document.write(x.getElementsByTagName("edition")[0].nodeName);
Sortie :
édition
Essayez-le vous-même »
Exemple 2
L'extrait de code suivant utiliseloadXMLDoc()pour charger "" dans xmlDoc, et ajoute un nouveau nœud à tous les éléments <book> :
Exemple
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);
x[i].appendChild(newel);
}
//Afficher tous les titres et les éditions
y=xmlDoc.getElementsByTagName("title");
z=xmlDoc.getElementsByTagName("edition");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write(" - Édition : ");
document.write(z[i].childNodes[0].nodeValue);
document.write("<br>");
}
Sortie :
Everyday Italian - Édition : Première
Harry Potter - Édition : Première
XQuery Kick Start - Édition : Première
Learning XML - Édition : Première
Harry Potter - Édition : Première
XQuery Kick Start - Édition : Première
Learning XML - Édition : Première
Essayez-le vous-même »
Objet Elementautres extensions