Jump to content

W3C XML DOM Tutorial


DominicWatson

Recommended Posts

Hi there, I was looking through the DOM tutorial and came to the creating element nodes example. The code for that looks like this:

xmlDoc=loadXMLDoc("books.xml");var x=xmlDoc.getElementsByTagName('book');var newel,newtextfor (i=0;i<x.length;i++){	 newel=xmlDoc.createElement('edition');	 newtext=xmlDoc.createTextNode('First');	 newel.appendChild(newtext);	 x[i].appendChild(newel);}

I don't think this code is very efficient as the node to get inserted into every book node has to be recreated for each book. However, I am very much a noob to this and am wondering if there is a problem with using cloneNode like this (or should one use the code in the W3C example)?

xmlDoc=loadXMLDoc("books.xml");var x=xmlDoc.getElementsByTagName('book');var newel = xmlDoc.createElement('edition');var newtext = xmlDoc.createTextNode('First');newel.appendChild(newtext);for (i=0;i<x.length;i++){	x[i].appendChild(newel.cloneNode(true));}

Thanks in advance,Dominic

Link to comment
Share on other sites

Well, yeah, the scripts at W3Schools are not exactly the most efficient, but they are not there to be such anyhow. They are there as examples. So, what you should use is what works best for you.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...