一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

javascript读取XML文件实现程序

时间:2013-01-24 编辑:简简单单 来源:一聚教程网

 代码如下 复制代码

 

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) { //火狐浏览器
            //else if (document.implementation && document.implementation.createDocument) {//这里主要是对谷歌浏览器进行处理
                xmlDoc = document.implementation.createDocument('', '', null);
                xmlDoc.load(xmlFile);
            }
            else{ //谷歌浏览器
              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
      

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); //保存
      
    }

热门栏目