通过javascript+DOM方式遍历XML节点(四)[原创] 

2007/7/27 13:30:00


阅读全文(3344) | 回复(1) | 编辑 | 精华

通过javascript+DOM方式遍历XML节点(四) ******************************************版权作者:Qr                            *成文时间:2007/07/27 13:30:00           *原创站点:http://Qr.blogger.org.cn      *版权声明:转载请保留版权信息***************************************** 代码说明:本代码仍沿用Recursion的思路,以for(无限)循环重写代码,性能上比用javascript的递归更胜一筹,但相对前两篇来讲,性能上改进不大,只是代码更简洁,结构更清晰了。JAVASCRIPT实现: <script language="javascript">/*****************************************************代码:Qr http://Qr.blogger.org.cn *时间:2007/07/15 22:50:00*功能:遍历XML的所有节点、属性,仿IE浏览XML方式输出*类型:支持TEXT、CDATA、COMMENT、ELEMENT节点****************************************************/var xml = null,format = "",fo = "&nbsp;&nbsp;&nbsp;&nbsp;";function main(){    createXmlDom("parser.xml");    xmlparser(xml.documentElement);}function createXmlDom(xmlUrl){    xml = new ActiveXObject("Msxml2.DOMDocument");    xml.async=false;    xml.load(xmlUrl);}function xmlparser(o) {for(;;){    if(o.nodeType==3 || o.nodeType==4 || o.nodeType==8){//o.hasChildNodes()==false        switch(o.nodeType){            case 3://[TEXT型节点]            if(o.nextSibling || o.previousSibling)document.write("<br>"+format)            document.write(o.nodeValue);            if(!o.nextSibling && o.previousSibling){//针对混合节点的末节点为文本节点的情况                document.write("<br>"+format.substr(0,format.length-24));            }            break;            case 4://[CDATA型节点]            document.write("<br>"+format+"&lt;![CDATA["+o.nodeValue+"]]&gt;");            break;            case 8://[COMMENT型节点]            document.write("<br>"+format+"&lt;!--"+o.nodeValue+"--&gt;");            break;        }        if(o.nextSibling){//#有同级兄弟元素节点#//            o=o.nextSibling;            continue;        }else{//#返回上级相邻未解析节点#//            o=N2PN(o.parentNode);            if(o==xml.documentElement)break;//避免死循环            continue;        }    }    if(!o.hasChildNodes()){//[空无素(ELEMENT)或仅含属性(ATTRIBUTE)]        if(o!=xml.documentElement)document.write("<br>");        if(o.nodeType==1)document.write(format+"&lt;"+o.nodeName);        if(o.attributes.length>0)attrparser(o);        document.write("/&gt;");        if(o.nextSibling){//#有同级兄弟元素节点#//            o=o.nextSibling;            continue;        }else{//#返回上级相邻未解析节点#//            if(o.previousSibling)//针对混合节点的末节点为空元素的情况                document.write("<br>"+format.substr(0,format.length-24));            o=N2PN(o.parentNode);            if(o==xml.documentElement)break;//避免死循环            continue;        }    }    if(o.hasChildNodes()){//有子节点,包括:TEXT、CDATA、COMMENT、ELEMENT        if(o!=xml.documentElement)document.write("<br>");        if(o.nodeType==1)document.write(format+"&lt;"+o.nodeName);        if(o.attributes.length>0)attrparser(o);        document.write("&gt;");        if(o.hasChildNodes()){            format += fo;            o=o.firstChild;//#有下级元素节点#//            continue;        }else{            if(o.nextSibling){//#有同级兄弟元素节点#//                o=o.nextSibling;                continue;            }        }    }}}function N2PN(p){    if(p.nextSibling){        format = format.substr(0,format.length-24);        //输出关闭节点名        if(p.nodeType==1 && p.firstChild.nodeType==3)            document.write("&lt;/"+p.nodeName+"&gt;");        if(p.nodeType==1 && p.firstChild.nodeType!=3)            document.write("<br>"+format+"&lt;/"+p.nodeName+"&gt;");        p = p.nextSibling;    }else{        for(;;){            format = format.substr(0,format.length-24);            //输出关闭节点名            if(p.parentNode && p.firstChild.nodeType==3)                document.write("&lt;/"+p.nodeName+"&gt;");            if(p.parentNode && p.firstChild.nodeType!=3)                document.write("<br>"+format+"&lt;/"+p.nodeName+"&gt;");            if(p==xml.documentElement)break;            p=p.parentNode;//如果没有上面一行,将遍历到#document            if(p.nextSibling){                format = format.substr(0,format.length-24);                document.write("<br>"+format+"&lt;/"+p.nodeName+"&gt;");                p = p.nextSibling;                break;            }            continue;        }    }    return p;}function attrparser(o){    if(o.attributes){        var attr = o.attributes;        for(i=0;i<attr.length;i++){            document.write(" "+attr[i].nodeName+'="'+attr[i].firstChild.nodeValue+'"');        }    }}main();</script> 写在最后:文章写到这里,与其说是教程,不如说是学习笔记。工科出身,文笔很差,见笑了。 基本上就这样了,只是到目前,如此这般(js+DOM)遍历XML节点有什么作用和意义我还是不明白,但这次写学习笔记,我更清楚的了解了DOM的运行机制和处理XML的方法,足够了! 现在都提倡面向对象的程序设计,javascript也不例外,我按照xmlparser的思路,也对代码进行了重构,只是没有一款好用的调试软件,遇到了一个问题至今未能查决,也不知道性能如何,看情况再说吧,希望代码能顺利通过调试,这样第5篇笔记就有机会让大家拍砖头了:-) javascript并非我的强项,只是写WEB系统要用到,这才临时抱佛脚,各位如有更好的遍历方法,请务必共享呵!另外,似乎javascript中的for...in语句也适合写这个程序,只是偶不太熟悉for...in语句来处理object,哪位熟悉的,写点简单代码给偶参考参考。 相关文章:通过javascript+DOM方式遍历XML节点(三)[原创] 通过javascript+DOM方式遍历XML节点(二)[原创] 通过javascript+DOM方式遍历XML节点(一)[原创]  

Qr

Posted by Qr on 2007/7/27 13:30:00

回复:通过javascript+DOM方式遍历XML节点(四)[原创]

2007/7/27 19:25:48


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除

晕晕晕。

烟雨朦胧

Posted by 烟雨朦胧 on 2007/7/27 19:25:48

» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.078 second(s), page refreshed 144757775 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号