火狐下javascript操作xml

字號(hào):


    上一個(gè)文章寫了ie操作xml,這次繼續(xù)火狐下javascript操作xml,火狐提供的xmldom比ie更加標(biāo)準(zhǔn),火狐下操作xmldom實(shí)際上就是它的javascript實(shí)現(xiàn),火狐實(shí)現(xiàn)了dom level2,而微軟的ie僅支持dom level1.
    1創(chuàng)建dom,dom標(biāo)準(zhǔn)指出,document.implementation對(duì)象有createdocument()方法.
    var forasp_cnxmldom = document.mplementation.createdocument(,,null);
    該方法的三個(gè)參數(shù)分別表示:文檔命名空間的url,文檔元素標(biāo)簽名 ,和一個(gè)文檔類型對(duì)象(總是null,因?yàn)榛鸷鼫缬讓?duì)文檔類型對(duì)象的支持)
    舉例
    var forasp_cnxmldom = document.mplementation.createdocument(http://www.ynpxrz.com,root,null);
    這就常見了一個(gè)<a0 root xmlns:a0=http://www.ynpxrz.com>的xml dom.
    a0表示命名空間
    2.載入xml,與微軟ie載入沒有l(wèi)oadxml()方法,只有l(wèi)oad()方法.load()方法與ie的load()方法相同.
    如果同步載入xml
    forasp_cnxmldom.async = false;
    forasp_cnxmldom.load(http://www.ynpxrz.com/rss.xml);
    如果以不再如則必須使用onload事件處理函數(shù)來判斷是否已經(jīng)載入.
    forasp_cnxmldom.onload = function(){alert(已經(jīng)載入);}
    forasp_cnxmldom.load(http://www.ynpxrz.com/rss.xml);
    3獲取xml,火狐提供了xmlserializer對(duì)象.
    var xmlobj = new xmlserializer();
    var xmlcontent = xmlobj.serializetostring(forasp_cnxmldom,text/xml);
    xmlserializer的唯一一個(gè)方法serializetostring(),參數(shù)是:序列化的節(jié)點(diǎn)和內(nèi)容類型(text/xml或者application/xml).為forasp_cnxmldom創(chuàng)建了xml代碼
    (這里不是很理解,待研究.)
    4.解析錯(cuò)誤
    在xml文件解析過程中發(fā)生錯(cuò)誤,xmldom會(huì)自動(dòng)創(chuàng)建文檔來解釋這個(gè)錯(cuò)誤.
    在此不多做研究.