XML DOM » Document » createElement

Syntax:
document.createElement(tagName)
tagName
Receives the type of the Element that will be created.

This method is used to create a new Element object of the type specified by the tagName argument.

Examples

Code:
XML:

<names>
   <name>Alice</name>
   <name>Bert</name>
   <name>Charlie</name>
   <name>Diane</name>
   <name>Eric</name>
</names>

VBScript:

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("names.xml")

Dim objCurrNode, objNewNode, objNewText
Set objNewNode = objXMLDoc.createElement("name")
Set objNewText = objXMLDoc.createTextNode("John")
objNewNode.appendChild(objNewText)

Set objCurrNode = objXMLDoc.documentElement
objCurrNode.appendChild(objNewNode)
Set objCurrNode = objCurrNode.lastChild
Response.write(objCurrNode.firstChild.nodeValue)
Output:
John
Explanation:

This example first loads the 'names.xml' file. It then creates a new 'name' element and a text node with the value of 'John' which is appended to it. The new 'name' element is then appended to the document's root element, and finally the variable objCurrNode is set to the root's last child (the newly-appended 'name' element), and the value of its first child (the text node) is displayed.

Language(s): VBScript XML

See Also: