XML DOM Create Node - XML DOM

How to create nodes in XML DOM?

In XML DOM new nodes can be created by using different methods. By these new methods a new element node, text node, comment node, CDATA section node and attribute node can be created. Already a newly created node exists in the object element, and then the new one replaces the older one.

Create new Element node

A new element node is created by createElement() method. If the newly created element node exists in the element object, the new one replaces the older one.

Syntax

Syntax to use the createElement() method is as follows −

Where,

  • var_name − is the user-defined variable name which holds the name of new element.
  • ("tagname") − is the name of new element node to be created.

Example

An XML document node.xml is parsed into an XML DOM object and a new element node PhoneNo is created in the XML document.

  • new_element = xmlDoc.createElement("PhoneNo"); creates the new element node <PhoneNo>
  • x.appendChild(new_element); x holds the name of the specified child node <FirstName> to which the new element node is appended.

Execution

The file is saved as createnewelement_example.htm on the server path and this file should be on the same path as that of node.xml in the server. The attribute value is obtained as PhoneNo as output.

Create new Text node

A new text node is created is created by createTextNode() method.

Syntax

Syntax to use createTextNode() is as follows −

Where,

  • var_name − it is the user-defined variable name which holds the name of new text node.
  • ("tagname") − within the parenthesis is the name of new text node to be created.

Example

An XML document node.xml is parsed into a XML DOM object and a new text node Im new text node is created in the XML document.

Details of the above code are as below −

  • create_e = xmlDoc.createElement("PhoneNo"); creates a new element <PhoneNo>.
  • create_t = xmlDoc.createTextNode("Im new text node"); creates a new text node "Im new text node".
  • x.appendChild(create_e); the text node, "Im new text node" is appended to the element, <PhoneNo>.
  • document.write(x.getElementsByTagName("PhoneNo")[0].childNodes[0].nodeValue); writes the new text node value to the element <PhoneNo>.

Execution

The file is saved as createtextnode_example.htm on the server path and this file should be on the same path as that of node.xml in the server. The attribute value is obtained as PhoneNo: Im new text node as output.

Create new Comment node

A new comment node is created by this method. Comment node is meant for simplifying the code functionality.

Syntax

Syntax to use createComment() is as follows −

Where,

  • var_name − is the user-defined variable name which holds the name of new comment node.
  • ("tagname") − is the name of the new comment node to be created.

Example

An XML document node.xml is parsed into an XML DOM object and a new comment node “Company is the parent node” is created in the XML document.

In the above example −

  • create_comment = xmlDoc.createComment("Company is the parent node") creates a specified comment line.
  • x.appendChild(create_comment) In this line, 'x' holds the name of the element <Company> to which the comment line is appended.

Execution

The file is saved as createcommentnode_example.htm on the server path and this file should be on the same path as that of node.xml in the server. The attribute value is obtained as Company is the parent node.

Create New CDATA Section Node

A new CDATA section node is created by this method of createCDATASection().If the newly created CDATA section node exists in the element object, it is replaced by the new one.

Syntax

Syntax to use createCDATASection() is as follows −

Where,

  • var_name − is the user-defined variable name which holds the name of new the CDATA section node.
  • ("tagname") − is the name of new CDATA section node to be created.

Example

An XML document node.xml is parsed into an XML DOM object and a new CDATA section node “Create CDATA Example” is created in the XML document.

In the above example −

  • create_CDATA = xmlDoc.createCDATASection("Create CDATA Example") creates a new CDATA section node, "Create CDATA Example"
  • x.appendChild(create_CDATA) here, x holds the specified element <Employee> indexed at 0 to which the CDATA node value is appended.

Execution

The file is saved as createcdatanode_example.htm on the server path and this file should be on the same path as that of node.xml in the server. The attribute value is obtained as Create CDATA Example.

Create new Attribute node

A new attribute node is created by the setAttributeNode() method. If the newly created attribute node already exists, a new one replaces it.

Syntax

Syntax to use the createElement() method is as follows −

Where,

  • var_name − is the user-defined variable name which holds the name of new attribute node.
  • ("tagname") − is the name of new attribute node to be created.

Example

An XML document node.xml is parsed into an XML DOM object and a new attribute node Section is created in the XML document

In the above example −

  • create_a=xmlDoc.createAttribute("Category") creates an attribute with the name <section>.
  • create_a.nodeValue="Management" creates the value "A" for the attribute <section>.
  • x[0].setAttributeNode(create_a) this attribute value is set to the node element <Employee> indexed at 0.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML DOM Topics