🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.xml
Article #248

JS��ȡXML(ת��)

#248
From: <Test/>
Date: Tue, 08 Jun 2010 16:00
178 lines
6478 bytes
���austin.js

<script type="text/javascript">
var Url="austin.xml";
var cdsales=new ActiveXObject("Microsoft.XMLDOM"); //����XmlDom����
cdsales.async=true; //�첽����
cdsales.load(Url);
cdsales.onreadystatechange= new function LoadedSales(){
    var txt="";
    if(cdsales.readyState==4){
     if(cdsales.parseError.errorCode != 0){
      txt="err";
     }
     else{
      var bi=cdsales.documentElement.selectNodes("item");
      if(bi!=null&&bi.length>0){
       for(var i=0;i<bi.length;i++){
        txt+="<li><a href="+bi[i].childNodes[1].text+" style=color:"+bi[i].childNodes[2].text+">"+bi[i].childNodes[0].text+"</a></li>";
       }
      }
      else{
       txt="LoadingErr...";
      }
     }
    }
    else{
     txt="Loading....";
    }
    document.write(txt);
}
</script> 
���austin.xml

<?xml version="1.0" encoding="gb2312"?>
<root>
    <item>
      <txt>�����IJ�������</txt>
      <url>http://hi.baidu.com/austin82</url>
      <color>green</color>
    </item>
    <item>
      <txt>Austin�IJ�������</txt>
      <url>http://hi.baidu.com/austin82</url>
      <color>blue</color>
    </item>
</root>

JS��ȡ�����xml�ļ��ڵ㼰���ԣ�����IE��FF��
����������Һ����Ұ����������ҵ��ˡ�

�ö��������Լ����ţ�����������һ���ˣ�^_^ |

book.html   �������£�

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<!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>
��������CSDN���ͣ�ת�������������http://blog.csdn.net/wyhappy612/archive/2008/09/14/2842601.aspx
--
Happy XMLing


�� ��Դ:��ˮľ���� newsmth.net��[FROM: 114.249.208.*]
Technical Details
Message-ID: <50I10Z$DC3@newsmth.net>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!newsfeed00.sul.t-online.de!t-online.de!border2.nntp.dca.giganews.com!nntp.giganews.com!news.neu.edu.cn!news.byr.edu.cn!news.newsmth.net!NEWSMTH

Show Full Headers
Complete Article Headers
X-Google-Thread: 115571,441037f310568149,start
X-Google-NewGroupId: yes
X-Google-Attributes: gidf7ebf7e2af,domainid0,public,usenet
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!newsfeed00.sul.t-online.de!t-online.de!border2.nntp.dca.giganews.com!nntp.giganews.com!news.neu.edu.cn!news.byr.edu.cn!news.newsmth.net!NEWSMTH
From: thinkhy@newsmth.net-SPAM.no (<Test/>)
Newsgroups: cn.bbs.comp.xml
Subject: JS��ȡXML(ת��)
Date: 08 Jun 2010 16:00:41 GMT
Organization: ˮľ����
Message-ID: <50I10Z$DC3@newsmth.net>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: XML/A/M.1276012835.h0
Lines: 177