javascript讀取XML檔案實現程式

來源:互聯網
上載者:User
 代碼如下 複製代碼

 

1、通過JS讀取XML檔案,主要是判斷各個瀏覽器 

View Code

// 載入xml文檔
       var loadXML = function (xmlFile) {
            var xmlDoc;
            if (window.ActiveXObject) {
                xmlDoc = new ActiveXObject('Microsoft.XMLDOM');//IE瀏覽器
                xmlDoc.async = false;
                xmlDoc.load(xmlFile);
            }
            else if (isFirefox=navigator.userAgent.indexOf("Firefox")>0) { //Firefox瀏覽器
            //else if (document.implementation && document.implementation.createDocument) {//這裡主要是對Google瀏覽器進行處理
                xmlDoc = document.implementation.createDocument('', '', null);
                xmlDoc.load(xmlFile);
            }
            else{ //Google瀏覽器
              var xmlhttp = new window.XMLHttpRequest();
                xmlhttp.open("GET",xmlFile,false);
                xmlhttp.send(null);
                if(xmlhttp.readyState == 4){
                xmlDoc = xmlhttp.responseXML.documentElement;
                }
            }

            return xmlDoc;
        }

        // 首先對xml對象進行判斷
      var  checkXMLDocObj = function (xmlFile) {
            var xmlDoc = loadXML(xmlFile);
            if (xmlDoc == null) {
                alert('您的瀏覽器不支援xml檔案讀取,於是本頁面禁止您的操作,推薦使用IE5.0以上可以解決此問題!');
                window.location.href = '../err.html';

            }
            return xmlDoc;
        }

2、將讀取到的xml檔案中的資料顯示到html文檔上

View Code
    <script type="text/javascript" language="javascript">
        var xmlDoc = checkXMLDocObj('../openClass.xml');//讀取到xml檔案中的資料
        var a = document.getElementsByTagName("a");//擷取所有的A標籤
        $(document).ready(function () {
              var nodes;
            if($.browser.msie){ // 注意各個瀏覽器之間的區別
             nodes = xmlDoc.getElementsByTagName('collage')[0].childNodes; //讀取XML檔案中需要顯示的資料
             }
             else if (isFirefox=navigator.userAgent.indexOf("Firefox")>0){
                nodes = xmlDoc.getElementsByTagName('collage')[0].children; //讀取XML檔案中需要顯示的資料
             }
             else{
                nodes = xmlDoc.getElementsByTagName('resource');
             }
           
             for (var i = 0; i < a.length; i++) {
                if (a[i].parentNode.nodeName == "SPAN") {
                    for (var j = 0; j < nodes.length; j++) {
                        var resource = nodes[j];
                        var url = resource.getAttribute('url');
                        var href=$(a[i]).attr("href");
                        if (href == url) {
                            var count = resource.getAttribute('click');
                            var span = document.createElement("div");
                            var str = document.createTextNode("點擊率:" + count);
                            span.appendChild(str);
                            var div = a[i].parentNode.parentNode;
                            div.appendChild(span);
                            break;
                        }
                    }
                }
            }
        });
                $(function(){ //通過get請求,將點擊率增加
                 $(a).mousedown(function(){
                             var href = $(this).attr("href");
                            $.get("../receive.ashx",{url:href,rd:Math.random()}, function (msg) {
                           
                            });
                        })
        }) 
    </script>  

3、通過更新ashx檔案在伺服器上更新對應的xml檔案

 

public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string src = context.Request.QueryString["url"];
        string path = context.Server.MapPath("openClass.xml"); //開啟xml檔案
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(path); //注意不能用Xmlload()方法
        XmlNodeList nodeslist = xmlDoc.SelectNodes("/collage/resource"); //找到對應的節點
        foreach (XmlNode node in nodeslist)
        {
            XmlElement xe = (XmlElement)node;
            if (xe.GetAttribute("url") == src)
            {
                int count = int.Parse(xe.GetAttribute("click"));
                count = count + 1;
                xe.SetAttribute("click", count.ToString()); //更新內容
            }
        }
        xmlDoc.Save(path); //儲存
      
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.