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

2007/7/27 13:02:50


阅读全文(4428) | 回复(0) | 编辑 | 精华

通过javascript+DOM方式遍历XML节点(三) ******************************************版权作者:Qr                            *成文时间:2007/07/27 13:02:00           *原创站点:http://Qr.blogger.org.cn      *版权声明:转载请保留版权信息***************************************** 代码说明:本代码纯粹改上一篇“通过javascript+DOM方式遍历XML节点(二)[原创]”的函数doparser的do...while循环为for(无限)循环遍历节点,性能上没有根本上的提高。JAVASCRIPT实现: <script language="javascript">/*****************************************************代码:Qr http://Qr.blogger.org.cn *时间:2007/07/15 22:23: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){            if(o!=xml.documentElement)format += fo;            document.write(format+"&lt;"+o.nodeName);            attrparser(o);             if(o.nextSibling && !o.firstChild){                document.write("/&gt;"+"<br>");                format = format.substr(0,format.length-24);                o = o.nextSibling;                continue;            }            if(!o.nextSibling && !o.firstChild){                document.write("/&gt;"+"<br>");                if(o==xml.documentElement)break;                format = format.substr(0,format.length-24);                for(;;){                    if(o==xml.documentElement || o.nextSibling)break;                    if(o.parentNode){                        document.write(format);                        document.write("&lt;/"+o.parentNode.nodeName+"&gt;"+"<br>");                        format = format.substr(0,format.length-24);                        o = o.parentNode;                    }                }                if(o==xml.documentElement)break;                o = o.nextSibling;                continue;            }else{                document.write("&gt;");                if(o.firstChild.nodeType!=3)document.write("<br>");            }        }         if(o.nodeType==4){            format += fo;            document.write(format+"&lt;![CDATA["+o.nodeValue+"]]&gt;"+"<br>");            format = format.substr(0,format.length-24);             if(!o.parentNode.nextSibling && o.parentNode.lastChild==o && o.parentNode.firstChild!=o){                document.write(format+"&lt;/"+o.parentNode.nodeName+"&gt;"+"<br>");                format = format.substr(0,format.length-24);            }             if((!o.nextSibling && !o.previousSibling) || (!o.nextSibling && o.parentNode.nextSibling)){                document.write(format+"&lt;/"+o.parentNode.nodeName+"&gt;"+"<br>");                format = format.substr(0,format.length-24);            }             if(o.nextSibling){                o = o.nextSibling;                continue;            }            if(o.parentNode.nextSibling){                o = o.parentNode.nextSibling;            }else{                 o=o.parentNode;                o=CC2E(o);                if(o==xml.documentElement)break;                o = o.nextSibling;                continue;            }            if(o==xml.documentElement){                document.write(format+"&lt;/"+o.nodeName+"&gt;"+"<br>");                break;            }else{continue;}        }         if(o.nodeType==8){            format += fo;            document.write(format+"&lt;!--"+o.nodeValue+"--&gt;"+"<br>");            format = format.substr(0,format.length-24);             if(!o.parentNode.nextSibling && o.parentNode.lastChild==o && o.parentNode.firstChild!=o){                document.write(format+"&lt;/"+o.parentNode.nodeName+"&gt;"+"<br>");                format = format.substr(0,format.length-24);            }             if((!o.nextSibling && !o.previousSibling) || (!o.nextSibling && o.parentNode.nextSibling)){                document.write(format+"&lt;/"+o.parentNode.nodeName+"&gt;"+"<br>");                format = format.substr(0,format.length-24);            }              if(o.nextSibling){                o = o.nextSibling;                continue;            }            if(o.parentNode.nextSibling){                o = o.parentNode.nextSibling;            }else{                o=o.parentNode;                o=CC2E(o);                if(o==xml.documentElement)break;                o = o.nextSibling;                continue;            }            if(o==xml.documentElement){                document.write(format+"&lt;/"+o.nodeName+"&gt;"+"<br>");                break;            }else{continue;}        }         if(o.nodeType==3){            if(o.nextSibling && !o.previousSibling)document.write("<br>");            if(o.nextSibling || o.previousSibling){format += fo;document.write(format);format = format.substr(0,format.length-24);}            document.write(o.nodeValue);            if(o.previousSibling && !o.nextSibling)document.write("<br>");            if(o.nextSibling){                o = o.nextSibling;                document.write("<br>");                continue;            }             o=CC2E(o);            if(o==xml.documentElement)break;            o = o.nextSibling;            continue;        }         if(o.firstChild){            o = o.firstChild;            continue;        }        if(o.nextSibling){            o = o.nextSibling;            continue;        }        o=N2PN(o);        if(o==xml.documentElement)break;        o = o.nextSibling;        continue;    }} function CC2E(oo){    for(;;){        if(oo==xml.documentElement || oo.nextSibling)break;         if(oo.parentNode){            if(oo.nodeType!=3)document.write(format);            if(oo.nodeType==3 && oo.previousSibling && !oo.nextSibling)document.write(format);            document.write("&lt;/"+oo.parentNode.nodeName+"&gt;"+"<br>");            format = format.substr(0,format.length-24);            oo = oo.parentNode;        }    }    return oo;}function CC2E0(oo){    for(;;){        if(oo==xml.documentElement)break;        oo = oo.parentNode;        if(oo.nextSibling){            document.write(format+"&lt;/"+oo.nodeName+"&gt;"+"<br>");            format = format.substr(0,format.length-24);            oo = oo.nextSibling;            break;        }        if(oo==xml.documentElement)break;    }    return oo;}function N2PN(np){    for(;;){        if(np==xml.documentElement || np.nextSibling)break;        if(np.parentNode)np = np.parentNode;    }    return np;}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> 相关文章:通过javascript+DOM方式遍历XML节点(一)[原创]通过javascript+DOM方式遍历XML节点(二)[原创]

Qr

Posted by Qr on 2007/7/27 13:02:50


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

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