兼容Firefox火狐浏览器的JS读取远程XML某节点的值

80酷酷网    80kuku.com

  最初的想法是用于注册,判断要注册的用户名是否重复。

参数:
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>


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: