XML DOM » Document » createNode

Compability: Internet Explorer

Syntax:
document.createNode(type, name, nameSpaceURI)
type
The type parameter must be a value that uniquely identifies the node type. This can be either the associated integer or string value. For details of node types, see the Node object.
name
The name parameter contains the new node's value for the nodeName property. For details of how this property relates to each node type, see the Node object. Various of these nodes types, such as NODE_COMMENT and NODE_TEXT, have a constant value for their node name which begins with a hash(#) character; the 'name' parameter is ignored in these cases. Where a node type does not have a name, you should pass the empty string: "".
nameSpaceURI
The nameSpaceURI parameter is a string defining the namespace URI (universal resource identifier). If specified, the node is created in the context of the 'nameSpaceURI' with the prefix specified on the node name. If the 'name' parameter does not have a prefix, this is treated as the default namespace. If you pass the empty string, "", for this parameter, the node is created within the special nameSpace of the current document. Creating a qualified node without specifying a non-empty 'nameSpaceURI' returns an error.

This method creates a node using the specified type, name and namespace.

You cannot create nodes of type NODE_DOCUMENT, NODE_DOCUMENT_TYPE, NODE_ENTITY or NODE_NOTATION.

For elements and entity references, when the 'nameSpaceURI' parameter is anything other than an empty string, and the 'name' parameter does not contain a prefix , the 'nameSpaceURI' is treated as the default namespace.

Attributes cannot be scoped to a default namespace, and other elements are not qualified to a particular namespace (they are treated as being from the namespace defined by the document itself). To demonstrate this method, the following example uses the 'currencies.xml' file and creates a new 'currency' element and a new text node to which it assigns the value of 'ITL Italian Lira'. The text node is appended to the new 'currency' element, and that in turn is inserted into an appropriate position among the root element's children. Finally, the getElementsByTagName method is used to create a NodeList of currency elements, and the code iterates through the collection displaying the text of each.

Examples

Code:
XML:

<currencies>
   <currency>CHF Swiss Francs</currency>
   <currency>DEM German Deutsche Marks</currency>
   <currency>GBP United Kingdom Pounds</currency>
   <currency>JPY Japanese Yen</currency>
   <currency>USD United States Dollars</currency>
</currencies>


JavaScript:

xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("currencies.xml");

root = xml_doc.documentElement;
new_currency = xml_doc.createNode(1, "currency", "");
currency_text = xml_doc.createNode(3, "", "");
currency_text.text = "ITL Italian Lira";
new_currency.appendChild(currency_text);
root.insertBefore(new_currency, root.childNodes.item(3));

currencies = xml_doc.getElementsByTagName("currency");
n_currencies = currencies.length;
for (i = 0; i < n_currencies; i++)
   document.write(currencies[i].text + "<br>");
Output:
CHF Swiss Francs
DEM German Deutsche Marks
GBP United Kingdom Pounds
ITL Italian Lira
JPY Japanese Yen
USD United States Dollars
Explanation:

This example uses the 'currencies.xml' file and creates a new 'currency' element and a new text node to which it assigns the value of 'ITL Italian Lira'. The text node is appended to the new 'currency' element, and that in turn is inserted into an appropriate position among the root element's children. Finally, the getElementsByTagName method is used to create a NodeList of currency elements, and the code iterates through the collection displaying the text of each.

Language(s): JavaScript XML

See Also: