The createElement() method creates the new element node.
The
following code fragment create an element (<edition>), and adds
it after the last child of each <book> element:
xmlDoc=loadXMLDoc("bookdetails.xml");
var x=xmlDoc.getElementsByTagName('book');
var newel;
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement('edition');
x[i].appendChild(newel);
}
Create an Attribute
The createAttribute() creates the new attribute node.
The
following code fragment create an "edition" attribute and adds
it to all <book> elements
xmlDoc=loadXMLDoc("bookdetails.xml");
var x=xmlDoc.getElementsByTagName('book');
var newatt;
for (i=0;i<x.length;i++)
{
newatt=xmlDoc.createAttribute("edition");
newatt.value="first";
x[i].setAttributeNode(newatt);
}
Create a Text Node
The createTextNode() method create a new text node.
The
following code fragment create an element (<edition>), with a
text node ('First') in it, and adds it after the last child of
each <book> element:
xmlDoc=loadXMLDoc("bookdetails.xml");
var x=xmlDoc.getElementsByTagName('book');
var newel,newtext;
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement('edition');
newtext=xmlDoc.createTextNode('First');
newel.appendChild(newtext);
x[i].appendChild(newel);
}
Create a CDATA Section Node
The createCDATASection() method create a new CDATA section
node.
The following code fragment create a CDATA section, and
adds it after the last child of each <book> element:
xmlDoc=loadXMLDoc("bookdetails.xml");
var x=xmlDoc.getElementsByTagName('book');
var newCDATA,newtext;newtext="Special Offer & Book Sale";
for (i=0;i<x.length;i++)
{
newCDATA=xmlDoc.createCDATASection(newtext);
x[i].appendChild(newCDATA);
}
Create a Comment Node
The createComment() method create a new comment node.
The
following code fragment create a comment node, and adds it
after the last child of each <book> element:
xmlDoc=loadXMLDoc("bookdetails.xml");
var x=xmlDoc.getElementsByTagName('book');
var newComment,newtext;
newtext="Revised September 2006";
for (i=0;i<x.length;i++)
{
newComment=xmlDoc.createComment(newtext);
x[i].appendChild(newComment);
}
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
xml document, xml file, xml parser, xmldom microsoft.xmldom, text node, xml dom attribute, document object model