<html> <head> <script type="text/javascript" src="/upload/file/xml/loadxmldoc.js"> </script> </head> <body> <script type="text/javascript"> //确定最后一个子节点为元素节点 function get_lastchild(\n) { var x=\n.lastChild; while (x.nodeType!=1) { x=x.previousSibling; } return x; } xmlDoc=loadXMLDoc("/upload/file/xml/books.xml"); var x=xmlDoc.documentElement; var newNode=xmlDoc.createElement("book"); var newTitle=xmlDoc.createElement("title"); var newText=xmlDoc.createTextNode("A Notebook"); newTitle.appendChild(newText); newNode.appendChild(newTitle); x.insertBefore(newNode,get_lastchild(x)); //输出所有title var y=xmlDoc.getElementsByTagName("title"); for (i=0;i<y.length;i++) { document.write(y[i].childNodes[0].nodeValue); document.write("<br />"); } </script> </body> </html>
编辑上面的文字,按下“查看结果”,效果会显示在右边 Create by 软晨网(RuanChen.com)