建站學院(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> |
上一篇 Access數據庫技術(18)
下一篇 Access數據庫技術(60)