js获取xml数据,兼容各大浏览器

 

function getXMLNode( str_xmlUrl, str_dirPath )
{  
        if( !str_xmlUrl || !str_dirPath )
                return false;
        var _bool_IE = ( window.ActiveXObject ? true : false );
        var _obj_xmlDoc;
        if( _bool_IE )
                _obj_xmlDoc = new ActiveXObject( 'Microsoft.XMLDOM' );
        else
                _obj_xmlDoc = document.implementation.createDocument("","",null);
               
        _obj_xmlDoc.async = false;
        try
        {
                _obj_xmlDoc.load( str_xmlUrl );  //ie 下 js获取xml
        }// try
        catch ( E )
        {
                return false;
        }// catch
        /* Mozila Firefox */
        if( !_bool_IE )
        {
                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;

                for( var _int_i = 0; _int_i < _arr_dirPath.length; _int_i ++ )
                {
                        var _int_localIndex = 0;
                        var _int_aimIndex = 0;
                        var _str_dirPath = _arr_dirPath[_int_i];
                        var _arr_result = _obj_reg.exec( _str_dirPath );
                        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 )
                                {
                                        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 );  //firefox下 js获取xml

        }// if
       
        /* Microsoft IE */
        try
        {
                return _obj_xmlDoc.selectNodes( str_dirPath )[0].text;
        }
        catch( e )
        {
                return false;
        }
        return false;
}
   
    alert(getXMLNode("http://www.baidu.com/?xml","data/pageId"));

 

博主推荐阅读:

1、张灵甫、黄伯韬等王牌为何会全军覆没

2、二战时期仅有的国军开坦克追日本兵,都压成肉酱了!!!

3、军事奇迹:看一书生7000人马横扫大半个中国的奇迹

4、国军悍将创造军事奇迹后为什么会投降日军?

5、新水浒传全集 下载地址 种子文件

6、flash无法覆盖的问题_三种办法可以解决flash覆盖其他元素

7、南海石油30年疯狂开采,到底还剩多少?

8、送礼国家也收税?莫不是都疯啦?

9、古代第一美女的悲惨结局

10、中国空军光棍节轰炸日本航母“龙骧”号

11、日本本土史上第一次空袭竟是中国轰炸机

12、中原大战中老将的王牌战将为何被害?

13、旅游行业_旅游产品 交易平台——比比西旅游平台