使用节点的关系来循环元素节点

  1. <html>
  2. <head>
  3. <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
  4. </head>
  5. <body>
  6.  
  7. <script type="text/javascript">
  8. xmlDoc=loadXMLDoc("/example/xdom/books.xml");
  9.  
  10. x=xmlDoc.getElementsByTagName("book")[0].childNodes;
  11. y=xmlDoc.getElementsByTagName("book")[0].firstChild;
  12. for (i=0;i<x.length;i++)
  13. {
  14. if (y.nodeType==1)
  15. {//Process only element nodes (type 1)
  16. document.write(y.nodeName + "<br />");
  17. }
  18. y=y.nextSibling;
  19. }
  20. </script>
  21. </body>
  22. </html>