Compability: Internet Explorer
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.
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>");CHF Swiss Francs
DEM German Deutsche Marks
GBP United Kingdom Pounds
ITL Italian Lira
JPY Japanese Yen
USD United States Dollars
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.