Dom Nodes Add
# XML DOM Adding Nodes
* * *

## Try It - Example
The following example uses the XML file [books.xml](#).
The function [loadXMLDoc()](#), located in an external JavaScript file, is used to load the XML file.
(#)
This example uses the appendChild() method to add a child node to an existing node.
(#)
This example uses the insertBefore() method to insert a node before a specified child node.
(#)
This example uses the setAttribute() method to add a new attribute.
(#)
This example uses insertData() to insert data into an existing text node.
* * *
## Adding Nodes - appendChild()
The appendChild() method adds a child node to an existing node.
The new node is added (appended) after any existing child nodes.
**Note:** If the position of the node is important, use the insertBefore() method.
The following code snippet creates an element () and adds it after the last child of the first element:
## Example
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book");
x.appendChild(newel);
[Try it Β»](#)
Example explanation:
1. Load "[books.xml](#)" into xmlDoc using [loadXMLDoc()](#)
2. Create a new node
3. Append this node to the first element
Traverse and append an element to all elements: (#)
* * *
## Inserting Nodes - insertBefore()
The insertBefore() method is used to insert a node before a specified child node.
This method is useful when the position of the added node is important:
## Example
xmlDoc=loadXMLDoc("books.xml");
newNode=xmlDoc.createElement("book");
x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book");
x.insertBefore(newNode,y);
[Try it Β»](#)
Example explanation:
1. Load "[books.xml](#)" into xmlDoc using [loadXMLDoc()](#)
2. Create a new element node
3. Insert this new node before the last element node
If the second parameter of insertBefore() is null, the new node will be added after the last existing child node.
**x.insertBefore(newNode,null)** and **x.appendChild(newNode)** both can append a new child node to x.
* * *
## Adding New Attributes
The addAtribute() method does not exist.
If the attribute does not exist, setAttribute() can create a new attribute:
## Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x.setAttribute("edition","first");
[Try it Β»](#)
Example explanation:
1. Load "[books.xml](#)" into xmlDoc using [loadXMLDoc()](#)
2. Set (create) the value of the "edition" attribute of the first element to "first"
**Note:** If the attribute already exists, the setAttribute() method will overwrite the existing value.
* * *
## Adding Text to a Text Node - insertData()
The insertData() method inserts data into an existing text node.
The insertData() method has two parameters:
* offset - Where to start inserting characters (starting from 0)
* string - The string to insert
The following code snippet will add "Easy" to the text node of the first element in the loaded XML:
## Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title").childNodes;
x.insertData(0,"Easy ");
[Try it Β»](#)
YouTip