IE和FireFox下,javascript讀取,並輸出xml檔案節點內容

來源:互聯網
上載者:User

javascript讀取xml檔案,輸出一個表格。
應用時候,根據xml檔案的實際格式,部分代碼需要調整

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
var xmlDoc;
var browse="";
function loadXML()
{
    var fileRoute="books.xml"
    if (window.ActiveXObject)
    {
        xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
        xmlDoc.async=false;
        xmlDoc.load(fileRoute);
        browse="ie";
    }
    else if (document.implementation && document.implementation.createDocument)
    {
        xmlDoc=document.implementation.createDocument('', '', null);
        xmlDoc.load(fileRoute);
        browse="ff";
    }
    else
    {
    alert( '未做與該瀏覽器的相容!');
    }
}

function getmessage()
{
    var msg='<table border="1" cellspacing="0" cellpadding="0" width="500">';
    msg+='<tr><td width="90"></td><td width="100">圖書編號</td><td width="100">種類編號</td><td width="110">圖書名稱</td><td width="100">作者</td></tr>'
    if(browse=="ff")
    {
        var cNodes = xmlDoc.getElementsByTagName("book");
        for(j=0;j<cNodes.length;j++)
        {
            var bookID=xmlDoc.getElementsByTagName("book")[j].getAttribute("id");
            var sortID=xmlDoc.getElementsByTagName("book")[j].getAttribute("sortID");
            var bookTitle=xmlDoc.getElementsByTagName("title")[j].childNodes[0].nodeValue;
            var bookAuthor=xmlDoc.getElementsByTagName("author")[j].childNodes[0].nodeValue;
            msg+='<tr><td>'+j+'</td><td>'+bookID+'</td><td width="100">'+sortID+'</td><td width="190">'+bookTitle+'</td><td width="120">'+bookAuthor+'</td></tr>'
        }
       
    }
    else if(browse=="ie")
    {
        var state = xmlDoc.readyState;
        if (state == 4)
        {
            var oNodes = xmlDoc.selectNodes("//books/book");
            for(j=0;j<oNodes.length;j++)
            {
                var bookID=oNodes[j].getAttribute("id");
                var sortID=oNodes[j].getAttribute("sortID");
                var bookTitle=oNodes[j].childNodes[0].text;
                var bookAuthor=oNodes[j].childNodes[1].text;
                msg+='<tr><td>'+j+'</td><td>'+bookID+'</td><td width="100">'+sortID+'</td><td width="190">'+bookTitle+'</td><td width="120">'+bookAuthor+'</td></tr>'
            }
        }
    }
    msg+='</table>';
    //alert(msg)
    document.getElementById("bookList").innerHTML=msg;
}

</script>
</head>
<body onload="loadXML();">
<div id="bookList" style="width:500px;">
</div>
<table border="0" cellspacing="0" cellpadding="0" width="500">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td> </td>
  </tr>
</table>
 <input name="button" type="button" onclick="getmessage()" value="onclick" />
</body>
</html>
 
books.xml
<?xml version="1.0" encoding="gb2312"?>
<books>
    <book id="4" sortID="a4">
        <title>author4aa</title>
        <author>author4</author>
    </book>
    <book id="5" sortID="a5">
        <title>author55</title>
        <author>author5</author>
    </book>
    <book id="6" sortID="a6">
        <title>booktitle</title>
        <author>author6</author>
    </book>
    <book id="7" sortID="a7">
        <title>booktitle</title>
        <author>author7</author>
    </book>
    <book id="8" sortID="a8">
        <title>booktitle</title>
        <author>author8</author>
    </book>
    <book id="15" sortID="a15">
        <title>author155</title>
        <author>author15</author>
    </book>
</books>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.