用ajax解析rss简单例子



< script  language ="javascript" >

var bloglist=new Array();
var xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
function startload(){
    xmlHttp.open(
"GET","http://www.cnblogs.com/zwl12549/Rss.aspx",true);
xmlHttp.onreadystatechange
=function(){
    
if(xmlHttp.readystate==4 && xmlHttp.status==200){
        
var xmldom=xmlHttp.responseXML;
        
var channel=xmldom.documentElement.childNodes.item(0);
        
var itemlist=channel.childNodes;
        
for(var i=0;i<itemlist.length;i++){
            
var node=itemlist.item(i);
            
if(node.tagName=="title"){
                blogtitle.innerHTML
=node.text;
            }

            
if(node.tagName=="description"){
                blogdescription.innerHTML
=node.text;
            }

            
if(node.tagName=="generator"){
                bloggenerator.innerHTML
=node.text;
            }

            
if(node.tagName=="item"){
                bloglist[bloglist.length]
=node.childNodes;
            }

        }

        showitem(bloglist[
0]);
    }

}

xmlHttp.send(
null);
}

function showitem(itemlist){
    
for(var i=0;i<itemlist.length;i++){
        
var node=itemlist.item(i);
        
if(node.tagName=="title"){
            itemtitle.innerHTML
=node.text;
        }

        
if(node.tagName=="link"){
            itemlink.href
=node.text;
        }

        
if(node.tagName=="description"){
            description.innerHTML
=node.text;
        }

        
if(node.tagName=="author"){
            author.innerHTML
=node.text;
        }

        
if(node.tagName=="pubDate"){
            pubDate.innerHTML
=node.text;
        }

    }

}

</ script >
< body  onload ="startload()" >
< font  color ="blue" >< id ="bloglink"  href ="#" >< div  id ="blogtitle" ></ div ></ a ></ font >< br  />
By 
< font  color ="blue" >< div  id ="bloggenerator" ></ div ></ font >< br  />
< font  color ="blur" >< div  id ="blogdescription" ></ div ></ font >< br  />
< br  />
< font  color ="#000066" >< id ="itemlink"  href ="#" >< div  id ="itemtitle" ></ div ></ a ></ font >< br  />
By 
< font  color ="blue" >< div  id ="author" ></ div ></ font >< br  />
< div  id ="description" ></ div >< br  />
< div  id ="pubDate"  align ="right" ></ div >
</ body >

你可能感兴趣的:(Ajax)