A real and useful way to get/prase XML file from a site

本文介绍了一种解决Firefox浏览器中跨域加载及解析远程XML文件的方法,通过使用netscape.security.PrivilegeManager.enablePrivilege实现跨域请求,并展示了如何通过XMLHttpRequest获取RSS源,最后解析并显示其中的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 真正的跨浏览器(主要是支持FireFox),远程加载并解析XMl文件。之前在网上看到很多说在FireFox下获取远程XMl文件的方法都无效,主要是因跨域问题。今天特此贴出本人用的一个方法,真正的街解决了问题,觉得好的朋友顶一个!

 

Code:

 

/*js psrt*/----file name:"custom.js"

 

 function getRss(){
     var url="http://cn.bing.com/news/search?form=QBLH&scope=news&filt=all&format=rss&q=s";
     var _item;
     var list = "";


            var xmlDoc;
            xhttp=new window.XMLHttpRequest();
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            xhttp.open("GET",url,true);
            xhttp.send(null);
            xhttp.onreadystatechange = function()
            {
                if(xhttp.readyState == 4)
                {
                    if(xhttp.status == 200)
                    {
                        xmlDoc=xhttp.responseXML;
                        items = xmlDoc.getElementsByTagName("item");
                        for(j = 0;j<items.length;j++)
                        {
                            _item = items[j];
                            title = _item.getElementsByTagName("title")[0];
                            list += "<p /><a href='#' id='titile_"+j+"' onclick='openDialog("+j+")'>"
                                    + title.firstChild.nodeValue +"</a></p>";
                        }       //end of for

             document.getElementById("show").innerHTML=list;
       

            }           //end of if
           else{alert("status is " + request.status);}
        }  //end of if
   }   //end of function
}  // end of getRss()

 

 

/*you can test with this html page under FF browser*/

 

<html>
<head>
<title>test</title>

 <script type="text/javascript" src="custom.js"></script>

</head>

<body>


        <input type="button" value="Search" class="Button" id="search" onclick="getRss()"/>

         <div id="show">the titles:</div>

</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值