zoukankan      html  css  js  c++  java
  • js读取XML文件

    转载至http://hi.baidu.com/zion_w/item/e0197f16e354ba27f6625cb6

    说明:我试过下面的方法,基本是可以的,但是下面标红的 xmlDoc.load(xmlFile);在firefox是没有问题的,但是在chrome它不可以,我查看到的原因xmlDoc的nodeType是9,nodeName是#document,它在chrome中不支持xmlDoc.load(xmlFile)。后来我换了一种方法,可以兼容各种浏览器。

        function loadXML(xmlFile) {
    
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.open("GET", xmlFile, false);
            xmlhttp.send();
            xmlDoc = xmlhttp.responseXML;
            return xmlDoc;
        }

    以下代码是在引用GOOGLE地图做地理系统的时候写的一小段。

    详细情况 ,请仔细读读吧,呵呵。

    JavaScript

    <script type="text/javascript">
        //<![CDATA[
        //嵌入CDATA段可以防止不兼容Javacript的浏览器不产生错误信息
        //增加正则表达式 
        String.prototype.getQueryString = function(name) {
            var reg = new RegExp("(^|&|\?)" + name + "=([^&]*)(&|$)"), r;
            if (r = this.match(reg)) return unescape(r[2]);
            return null;
        };
        var address = location.search.getQueryString("address"); //通过表达式获得传递参数
        //针对两种浏览器,分别获取xmlDocument对象// 读取XML文件   
        function loadXML(xmlFile) {
            var xmlDoc;
            if (window.ActiveXObject) {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.load(xmlFile);
            }
            else if (document.implementation && document.implementation.createDocument) {
                xmlDoc = document.implementation.createDocument("", "", null);
                xmlDoc.async = false;
                xmlDoc.load(xmlFile);
            } else {
                alert('您的浏览器不支持该系统脚本!');
            }
            return xmlDoc;
        }   
        //调用地图
        var map; //全局GMap GOOGLE 地图 API
        function load() {
            if (GBrowserIsCompatible()) //检查浏览器兼容性
            {
                map = new GMap2(document.getElementById("map")); //地图加栽到ID为map的DIV中。
                map.addControl(new GSmallMapControl());   //添加Gcontrol控件//放大缩小的那个
                map.setCenter(new GLatLng(26.577014, 104.877977), 15); //设置地图中心
                //创建多个坐标点(从INFO.XML文件中读取)
                var xmlDoc = loadXML("Info.xml"); 
                var members = xmlDoc.getElementsByTagName("number");
                var maxRes = members.length;
                for (var i = 0; i <= maxRes; i++) {                       //XML中记录了多个坐标点,要每个点都标记一下
                    var oName = members[i].getElementsByTagName("name");
                    var oLongitude = members[i].getElementsByTagName("Longitude");
                    var oLatitude = members[i].getElementsByTagName("Latitude");

                    var name = oName[0].firstChild.nodeValue
                    var Longitude = oLongitude[0].firstChild.nodeValue
                    var Latitude = oLatitude[0].firstChild.nodeValue        
                    
                    var marker = new GMarker(new GLatLng(Longitude, Latitude), { title: name });     //对每个点添加标记
                    marker.openInfoWindowHtml("<div style=line-height:20px;text-align:center;font-size:12px;'><a href=Left.aspx?info=" + name + " target=framLeft>" + name + ",点击查看信息</a></div>"); 
                    map.addOverlay(marker);
                }
            }
        }
        //]]>
    </script>

    XML文件

    <?xml version="1.0" encoding="GB2312"?>   
    <earth>   
    <number id='1'>    
    <name>213211212213213</name>   
    <Longitude>26.577014</Longitude>   
    <Latitude>104.877977</Latitude></number>   
    <number id='2'>    
    <name>112312332131212</name>   
    <Longitude>26.586685</Longitude>   
    <Latitude>104.863815</Latitude></number>   
    <number id='3'>    
    <name>123123121323112</name>   
    <Longitude>26.572101</Longitude>   
    <Latitude>104.866905</Latitude></number>   
    <number id='4'>    
    <name>123132123123321</name>   
    <Longitude>26.572254</Longitude>   
    <Latitude>104.891624</Latitude></number>   
    </earth>

  • 相关阅读:
    MYSQL一对多,两表查询合并数据
    bootstrap瀑布流代码
    os mac apache+php+mysql环境配置
    centos 6.5 服务器安装 (LNMP ntfs文件支持 PHP-RPM CHROOT沙盒)
    在linux下将当前目录文件全部小写含目录名
    Javascript知识汇总------js中容易被忽略的细节(持续更新)
    Javascript知识汇总------js数据类型隐式转换
    下次要写约瑟夫和并查集
    --wrong answer
    --最小生成树
  • 原文地址:https://www.cnblogs.com/xiang1336/p/3493569.html
Copyright © 2011-2022 走看看