案例: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 first child node is an element node
10
function get_firstchild(n)
11
{
12
var x=n.firstChild;
13
while (x.nodeType!=1)
14
  {
15
  x=x.nextSibling;
16
  }
17
return x;
18
}
19
xmlDoc=loadXMLDoc("/example/xdom/books.xml");
20
21
//create a title element and a text node
22
var newNode=xmlDoc.createElement("title");
23
var newText=xmlDoc.createTextNode("Giada's Family Dinners");
24
//add the text node to the title node,
25
newNode.appendChild(newText);
26
27
//replace the first child node with the new node
28
var x=xmlDoc.getElementsByTagName("book")[0];
29
x.replaceChild(newNode,get_firstchild(x));
30
31
//output all titles
32
var y=xmlDoc.getElementsByTagName("title");
33
for (i=0;i<y.length;i++)
34
{
35
document.write(y[i].childNodes[0].nodeValue);
36
document.write("<br />");
37
}
38
39
</script>
40
</body>
41
</html>
42