javascript读取xml文件

 
核心部分
  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
  2. <script language="javascript">
  3.     //创建Dom对象
  4.     function createDocument() {
  5.         var aVersions = ["MSXML2.DOMDocument.5.0",
  6.                         "MSXML2.DOMDocument.4.0",
  7.                         "MSXML2.DOMDocument.3.0",
  8.                         "MSXML2.DOMDocument",
  9.                         "Microsoft.XmlDom"
  10.                         ];
  11.           for (var i = 0; i < aVersions.length; i++) {
  12.              try {
  13.                  var oXmlDom = new ActiveXObject(aVersions[i]);
  14.                  return oXmlDom;
  15.              } catch (oError) {
  16.           }
  17.         }
  18.         throw new Error("MSXML is not installed.");
  19.     }
  20. function init() {
  21.     var oXmlDom = createDocument();
  22.     oXmlDom.async = true;
  23.     oXmlDom.onreadystatechange = function () {
  24.         if (oXmlDom.readyState == 4) {
  25.                 if (oXmlDom.parseError.errorCode == 0) {
  26.                     parseBookInfo(oXmlDom);
  27.                 }else{
  28.                     var str = "An error occurred!!/n" +
  29.                     "Description: " + oXmlDom.parseError.reason + "/n" +
  30.                     "File: " + oXmlDom.parseError.url + "/n" +
  31.                     "Line: " + oXmlDom.parseError.line + "/n" +
  32.                     "Line Position: " + oXmlDom.parseError.linePos + "/n" +
  33.                     "Source Code: " + oXmlDom.parseError.srcText;
  34.                     alert(str);
  35.                 }       
  36.             }
  37.     };
  38.     oXmlDom.load("books.xml");
  39. }   
  40.     //文档解析
  41.     function parseBookInfo(oXmlDom) {
  42.         var oRoot = oXmlDom.documentElement;
  43.         var oFragment = document.createDocumentFragment();
  44.         var aBooks = oRoot.getElementsByTagName("book");
  45.         //遍历生成每个信息
  46.         for (var i = 0; i < aBooks.length; i++) {
  47.             var sIsbn = aBooks[i].getAttribute("isbn");
  48.             var sAuthor, sTitle, sPublisher;
  49.             var oCurrentChild = aBooks[i].firstChild;
  50.             do {
  51.                 switch (oCurrentChild.tagName) {
  52.                     case "title":sTitle = oCurrentChild.text;           break;
  53.                     case "author":sAuthor = oCurrentChild.text;         break;
  54.                     case "publisher":sPublisher = oCurrentChild.text;   break;
  55.                     default: break;
  56.                 }
  57.             }while(oCurrentChildoCurrentChild = oCurrentChild.nextSibling);
  58.             
  59.             //---------------生成显示页面--------------
  60.             //生成每个xml元素的基本容器
  61.             var divContainer = document.createElement("div");
  62.             var imgBookCover = document.createElement("img");
  63.             var divContent = document.createElement("div");
  64.             var sOdd = (i % 2)?"":"-odd";
  65.             divContainer.className = "bookContainer" + sOdd;
  66.             
  67.             //封面信息
  68.             imgBookCover.src = "images/" + sIsbn + ".jpg";
  69.             imgBookCover.className = "bookCover";
  70.             divContainer.appendChild(imgBookCover);
  71.             //标题
  72.             var h3Title = document.createElement("h3");
  73.             h3Title.appendChild(document.createTextNode(sTitle));
  74.             divContent.appendChild(h3Title);
  75.             //作者
  76.             divContent.appendChild(document.createTextNode("Written by: " + sAuthor));
  77.             divContent.appendChild(document.createElement("br"));
  78.             divContent.appendChild(document.createTextNode("ISBN: #" + sIsbn));
  79.             //出版社
  80.             var divPublisher = document.createElement("div");
  81.             divPublisher.className = "bookPublisher";
  82.             divPublisher.appendChild(document.createTextNode("Published by: " + sPublisher));
  83.             divContent.appendChild(divPublisher);
  84.             //组装
  85.             divContent.className = "bookContent";
  86.             divContainer.appendChild(divContent);
  87.             oFragment.appendChild(divContainer);
  88.             
  89.             document.body.appendChild(oFragment);
  90.     }
  91. }
  92. </script>
  93.     
  94.     
  95. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  96. <html xmlns="http://www.w3.org/1999/xhtml" >
  97. <head>
  98.     <title>Book XML Exercise</title>
  99.     <link rel="stylesheet" type="text/css" href="books.css" />
  100.     <script type="text/javascript" src="zxml.js"></script>
  101. </head>
  102.     <body onload="init()">
  103.     </body>
  104. </html>
 
books.xml
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <bookList>
    3.     <book isbn="0471777781">
    4.        <title>Professional Ajax</title>
    5.        <author>Nicholas C. Zakas, Jeremy McPeak, Joe Fawcett</author>
    6.        <publisher>Wrox</publisher>
    7.     </book>
    8.     <book isbn="0764579088">
    9.        <title>Professional JavaScript for Web Developers</title>
    10.        <author>Nicholas C. Zakas</author>
    11.        <publisher>Wrox</publisher>
    12.     </book>
    13.     <book isbn="0764557599">
    14.         <title>Professional C#</title>
    15.         <author>Simon Robinson, et al</author>
    16.         <publisher>Wrox</publisher>
    17.     </book>
    18.     <book isbn="1861006314">
    19.        <title>GDI+ Programming: Creating Custom Controls Using C#</title>
    20.        <author>Eric White</author>
    21.        <publisher>Wrox</publisher>
    22.     </book>
    23.     <book isbn="1861002025">
    24.        <title>Professional Visual Basic 6 Databases</title>
    25.        <author>Charles Williams</author>
    26.        <publisher>Wrox</publisher>
    27.   </book>
    28. </bookList>
  1. css
    1. body{
    2.     font-size:12px;
    3.     text-align:left;
    4. }
    5. div{
    6. }
    7. .bookContainer-odd{
    8.     background-color:#CCCCFF;
    9.     border:1px solid #000000;
    10.     padding:5px;
    11.     margin:5px;
    12.     width:600px;
    13. }
    14. .bookContainer{
    15.     background-color:#CC99FF;
    16.     border:1px solid #000000;
    17.     padding:5px;
    18.     margin:5px;
    19.     width:600px;
    20. }
    21. .bookCover{
    22.     max-height:160px;
    23.     max-width:200px
    24.     border:none;
    25. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值