加载并解析 XML 字符串

  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="<bookstore>"
  8. text=text+"<book>";
  9. text=text+"<title>Harry Potter</title>";
  10. text=text+"<author>J K. Rowling</author>";
  11. text=text+"<year>2005</year>";
  12. text=text+"</book>";
  13. text=text+"</bookstore>";
  14.  
  15. xmlDoc=loadXMLString(text);
  16.  
  17. document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue);
  18. document.write("<br />");
  19. document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue);
  20. document.write("<br />");
  21. document.write(xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue);
  22. </script>
  23. </body>
  24. </html>