詳細介紹闡述js獲取xml文件中的數(shù)據(jù)的三種方法
文章來源:365jz.com 點擊數(shù):
1430 更新時間:2009-09-24 22:28
參與評論
javascript操作xml文件可以實現(xiàn)創(chuàng)建,刪除,插入節(jié)點,查詢節(jié)點的屬性值等,請看用Javascript 對XML進行訪問,創(chuàng)建,插入,刪除四動作
這里只講怎么樣利用js獲取XML文件中的數(shù)據(jù)
舉例說明
先創(chuàng)建一個xml文件
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<eyejs>
<options catename="js學習">
<options1 title="第一個分類">
<name>基礎知識區(qū)</name>
<url>http://www.eyejs.com/html/16/category-catid-16.html</url>
</options1>
<options2>
<name>兼容ie 和ff瀏覽器的代</name>
<url>http://www.eyejs.com/html/17/category-catid-17.html</url>
</options2>
<options3>
<name>js小技巧區(qū)</name>
<url>http://www.eyejs.com/html/12/category-catid-12.html</url>
</options3>
</options>
<options catename="js組件"></options>
<regedit name="注冊"></regedit>
</eyejs>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*1. 第一步肯定是要創(chuàng)建一個操作xml文件的對象
可是IE和FF創(chuàng)建的方法不一樣,所以要寫個兼容IE和FF的
2. 加載XML文件
3. 獲取或設置XML中的數(shù)據(jù)有如下方法
利用selectNodes或childNodes或
a. 對象.documentElement.childNodes.item(0) 根元素的直接子級第一個元素對象,依次累推......
獲取是用text或getAttribute("屬性名"),設置是用setAttribute("屬性名",值)或text
b. 對象.documentElement.selectNodes("http://根元素/直接子級1")或selectSingleNode
c. 對象.getElementsByTagName()
*/
創(chuàng)建一個操作xml文件的對象的方法
function createXMLObj(xmlPath){
if(window.ActiveXObject){ //在IE下
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load(xmlPath);
}else if(document.implementation && document.implementation.createDocument){ //在FF下
xmlDoc=document.implementation.createDocument("", "", null);
xmlDoc.load(xmlPath);
}else{
return null;
}
return xmlDoc;
}
獲取數(shù)據(jù)的三種方法
//方法一
//document.writeln( xdoc.documentElement.childNodes.item(2).childNodes.item(0).getAttribute("title") );
//方法二
//document.writeln( xdoc.documentElement.selectNodes("http://eyejs/options/options1").item(0).childNodes.item(0).text );
//document.writeln( xdoc.documentElement.selectNodes("http://eyejs/options/options1").item(0).getAttribute("title") );
//document.writeln( xdoc.documentElement.selectNodes("http://eyejs/options/options1").item(0).attributes(0).value );
//方法三
//document.writeln( xdoc.getElementsByTagName("options")[0].getAttribute("catename") );
//document.writeln( xdoc.getElementsByTagName("options")[0].attributes[0] );
在獲取數(shù)據(jù)時,在FF下一定要用xdoc.onload=function(){ }來獲取
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
舉例js代碼
function createXMLObj(xmlPath){
if(window.ActiveXObject){ //在IE下
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load(xmlPath);
getValue(xmlDoc);
}else if(document.implementation && document.implementation.createDocument){ //在FF下
xmlDoc=document.implementation.createDocument("", "", null);
xmlDoc.load(xmlPath);
xmlDoc.onload=function(){ getValue(xmlDoc); }
}else{
return null;
}
return xmlDoc;
}
function getValue(xmlDoc){
var arrTags=xmlDoc.getElementsByTagName("options1");
document.writeln( arrTags[0].getAttribute("title") );
}
完整代碼下載:http://www.eyejs.com/html/21/n-221.html
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答??! 點擊進入論壇
------分隔線----------------------------