案例:XML DOM案例     状态:可编辑再运行    进入竖版
 运行结果 
x
 
1
<html>
2
<head>
3
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
4
</script>
5
</head>
6
<body>
7
8
<script type="text/javascript">
9
//check if the last childnode is an element node
10
function get_lastchild(n)
11
{
12
x=n.lastChild;
13
while (x.nodeType!=1)
14
  {
15
  x=x.previousSibling;
16
  }
17
return x;
18
}
19
20
xmlDoc=loadXMLDoc("/example/xdom/books.xml");
21
x=xmlDoc.getElementsByTagName("book")[0];
22
23
newNode=xmlDoc.createElement("publisher");
24
newText=xmlDoc.createTextNode("Clarkson Potter");
25
26
newNode.appendChild(newText);
27
x.insertBefore(newNode,get_lastchild(x));
28
29
x=x.childNodes;
30
31
for (i=0;i<x.length;i++)
32
{ 
33
if (x[i].nodeType==1)
34
  { 
35
  //Process only element nodes
36
  document.write(x[i].nodeName);
37
  document.write(" - ");
38
  document.write(x[i].childNodes[0].nodeValue);
39
  document.write("<br />");
40
  } 
41
}
42
43
</script>
44
</body>
45
</html>