May 27, 2021 XML DOM
The following example uses the XML
file .xml.
The
function loadXMLDoc(),
located in an external JavaScript, is used to load XML files.
Add a node after the last child node
This example uses the appendChild() method to add a child node to an existing node.
Add a node before the specified child node
This example uses the insertBefore() method to insert a node before a specified child node.
Add a new property
This example uses the setAttribute() method to add a new property.
Add data to the text node
This example uses insertData() to insert data into an existing text node.
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 location of the node is important, use the insertBefore() method.
The following snippet creates an element and adds it to the last child node of the first element:
Example explanation:
Traverse and append an element to all the elements: Try
The insertBefore() method is used to insert nodes before a specified child node.
This method is useful when the location of the added node is important:
Example explanation:
If the second parameter of insertBefore() is null, the new node is added after the last existing child node.
x.insertBefore (newNode, null) and x.appendChild (newNode) can add a new child node to x.
AddAtribute() This method does not exist.
If the property does not exist, setAttribute() creates a new property:
Example explanation:
Note: If the property already exists, the setAttribute() method overrides the existing value.
The insertData() method inserts data into an existing text node.
The insertData() method has two parameters:
The following snippet will add "Easy" to the text node of the first element of the loaded XML: