HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The appendChild() method adds a child node to an existing node
The appendChild() method adds a child node to an existing node.
The new node is added (appended) after any existing child nodes.
This code fragment creates an element (<edition>), and adds it after the last child of the first <book> element:
newEle = xmlDoc.createElement("edition");
xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
Example explained:
This code fragment does the same as above, but the new element is added with a value:
newEle = xmlDoc.createElement("edition");
newText=xmlDoc.createTextNode("first");
newEle.appendChild(newText);
xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
Example explained:
The insertBefore() method inserts a node before a specified child node.
This method is useful when the position of the added node is important:
newNode = xmlDoc.createElement("book");
x = xmlDoc.documentElement;
y = xmlDoc.getElementsByTagName("book")[3];
x.insertBefore(newNode,y);
Example explained:
If the second parameter of insertBefore() is null, the new node will be added after the last existing child node.
The setAttribute() method sets the value of an attribute.
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
Example explained:
There is no method called add Attribute()
The setAttribute() will create a new attribute if the attribute does not exist.
The insertData() method inserts data into an existing text node.
The insertData() method has two parameters:
The following code fragment will add "Easy" to the text node of the first <title> element of the loaded XML:
xmlDoc.getElementsByTagName("title")[0].childNodes[0].insertData(0,"Easy ");