遍历一棵节点树

  1. <html>
  2. <head>
  3. <script type="text/javascript" src="/example/xdom/loadxmlstring.js"></script>
  4. </head>
  5. <body>
  6. <script type="text/javascript">
  7. text="<book>";
  8. text=text+"<title>Harry Potter</title>";
  9. text=text+"<author>J K. Rowling</author>";
  10. text=text+"<year>2005</year>";
  11. text=text+"</book>";
  12.  
  13. xmlDoc=loadXMLString(text);
  14.  
  15. // documentElement always represents the root node
  16. x=xmlDoc.documentElement.childNodes;
  17. for (i=0;i<x.length;i++)
  18. {
  19. document.write(x[i].nodeName);
  20. document.write(": ");
  21. document.write(x[i].childNodes[0].nodeValue);
  22. document.write("<br />");
  23. }
  24. </script>
  25. </body>
  26. </html>