多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > web前端 > jscript > JS讀取遠程XML某節點的值(兼容Firefox)

JS讀取遠程XML某節點的值(兼容Firefox)

來源:程序員人生   發布時間:2013-11-15 21:27:49 閱讀次數:3428次

  建站學院(LieHuo.Net)技術文檔 本文主要介紹一篇兼容Firefox火狐瀏覽器的JS讀取遠程XML某節點的值參數的實例,希望對您有所幫助。

  參數:
  str_xmlUrl:遠程XML的地址;如http://192.168.1.19/test/xml.xml
  str_dirPath:要尋找的節點的路徑,如XMLDocument/test[0]/newName2/childs[2]

  JS代碼:

以下為引用的內容:
<script type="text/javascript">
function getXMLNode( str_xmlUrl, str_dirPath )

    if( !str_xmlUrl || !str_dirPath )
        return false;
    
    var _bool_IE = ( window.ActiveXObject ? true : false );//獲取瀏覽器類型
    
    //初始化XMLDocument對象
    var _obj_xmlDoc;
    if( _bool_IE )
        _obj_xmlDoc = new ActiveXObject( ’Microsoft.XMLDOM’ );
    else
        _obj_xmlDoc = document.implementation.createDocument("","",null);
        
    /*設置異步處理
        本函數不需要在XML文件讀取完成之前進行任何操作,
        因此關閉異步處理功能。
    */
    _obj_xmlDoc.async = false;
    //讀取XML文件內容
    try
    {
        _obj_xmlDoc.load( str_xmlUrl );
    }//try
    catch ( E )
    {
        //讀取失敗
        return false;
    }//catch
    /*根據路徑解析XML文件內容*/
    /*Mozila Firefox*/
    if( !_bool_IE )
    {
        /*
        解析DOM路徑并驗證
        要求該路徑使用"/"作為分隔符
        路徑內可使用"[n]"的方式指定子節點的索引
        如"XMLDocument/item[1]"表示根節點下的第一個XMLDocument節點下的第二個item節點
        如果不使用這種方式默認為第一個節點
        */
        var _arr_dirPath = str_dirPath.split( ’/’ );
        if( !_arr_dirPath[0] )
            _arr_dirPath.shift();
        if( !_arr_dirPath )
            return false;
            
        var _obj_aimElement = _obj_xmlDoc;
        //定義用于獲取節點索引的正則表達式對象
        var _obj_reg = /[([0-9]+)]/ig;
        /*根據DOM路徑解析獲取節點*/
        for( var _int_i = 0; _int_i < _arr_dirPath.length; _int_i ++ )
        {
            //當前符合條件的節點索引
            var _int_localIndex = 0;
            //DOM路徑中指定的節點索引
            var _int_aimIndex = 0;
            //獲取當前要解析的節點的相對路徑
            var _str_dirPath = _arr_dirPath[_int_i];
            //獲取當前要解析的節點的索引
            var _arr_result = _obj_reg.exec( _str_dirPath );
            //搜索標識,如果該標識為false則說明未獲得指定的節點
            var bool_catch = false;
            //如果指定了節點索引編號則整理節點相對路徑與節點索引
            if( _arr_result )
            {
                _int_aimIndex = _arr_result[1];
                _str_dirPath = _str_dirPath.replace( _arr_result[0], ’’ );
            }//if
            
            //獲取當前節點的全部子節點
            var _arr_childNodes = _obj_aimElement.childNodes;
            //在當前節點的子節點中查詢與當前要解析的節點的"相對路徑"及節點索引號匹配的節點
            for( var _int_j = 0; _int_j < _arr_childNodes.length; _int_j ++ )
            {
                //子節點與路徑匹配
                if( _arr_childNodes[_int_j].nodeName == _str_dirPath )
                {
                    //如果索引與路徑匹配則取得該節點
                    //否則將當前符合條件的節點索引加"1"
                    if( _int_localIndex == _int_aimIndex )
                    {
                        _obj_aimElement = _arr_childNodes[_int_j];
                        bool_catch = true;
                        break;
                    }//if
                    else
                        _int_localIndex += 1;
                }//if
            }//for
            //如果未獲得指定節點則返回錯誤
            if( !bool_catch )
            {
                return false;
            }
        }//for
        //返回搜索到的節點內容
        return( _obj_aimElement.childNodes[0].nodeValue );
    }//if
    
    /*Microsoft IE*/
    try
    {
        //返回搜索到的節點內容
        return _obj_xmlDoc.selectNodes( str_dirPath )[0].text;
    }
    catch( e )
    {
        return false;
    }
    return false;
}//function getXMLNode()
alert( getXMLNode( ’http://192.168.1.19/test/xml.xml’, ’XMLDocument/test[0]/newName2/childs[2]’ ) );
</script>

  XML代碼:

以下為引用的內容:
<?xml version="1.0" encoding="utf-8" ?> 
<XMLDocument>
<test>
<newName>
<name>name</name> 
<childs>test000</childs> 
<childs>test998</childs> 
<childs>test997</childs> 
<childs>
<childs>test889</childs> 
<childs>test888</childs> 
</childs>
</newName>
<newName2>
<childs>test996</childs> 
<childs>test995</childs> 
<childs>test994</childs> 
</newName2>
</test>
<newName3>
<childs>test993</childs> 
<childs>test992</childs> 
<childs>test991</childs> 
</newName3>
<STR_WEB_HOST>http://localhost:8080/WebTemp/</STR_WEB_HOST> 
<STR_SYS_CHARSET>utf-8</STR_SYS_CHARSET> 
</XMLDocument>

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: aa级一级天堂片免费观看 | 手机看一级片 | 国产精品特黄毛片 | 亚洲 欧美综合小说区图片区 | 最新欧美日韩 | 久久精品2020 | 欧美人与牲动交xxxx小说 | 国产日韩欧美中文字幕 | 免费 欧美 自拍 在线观看 | 日本xxwwwxxxx18| 欧美三级真做在线观看 | 天堂tv亚洲tv日本tv不卡 | 一二三四日本手机高清视频 | 最新中文字幕乱码在线 | 日韩看片| 亚洲无av码一区二区三区 | 中文字幕一区二区三区在线播放 | 午夜视频在线网站 | 精品国产区 | 国产淫视| 高清在线亚洲精品国产二区 | 久久国产精品久久 | 岛国在线123456 | 国产成人久久精品推最新 | 亚洲国产日韩欧美一区二区三区 | 最近最新中文字幕大全免费7 | 中文国产成人精品久久96 | 欧美亚洲综合另类成人 | 国产91色在线 | 亚洲 | 精品国产三级v | 久久最新精品 | 午夜成人影片 | s级毛片 | 欧美性狂猛bbbbbbxxxx | xxxx69欧美hdxxxhd| 亚洲一区 欧美 | 欧美日本一区二区三区生 | 琪琪五月天 | 夜夜骑狠狠干 | 国产免费一区二区三区在线观看 | 日本理论在线观看被窝网 |