通过javascript+DOM方式遍历XML节点(二)[原创]
2007/7/27 11:00:00
阅读全文(7495) | 回复(0) | 编辑 | 精华
通过javascript+DOM方式遍历XML节点(二) ******************************************版权作者:Qr *成文时间:2007/07/27 11:00:00 *原创站点:http://Qr.blogger.org.cn *版权声明:转载请保留版权信息 ***************************************** 代码说明:本代码仍沿用“通过javascript+DOM方式遍历XML节点(一)[原创]”的Recursion函数的思路,但摒弃的javascript的递归,改用do...while循环遍历节点。就处理XML大小来讲,性能上更胜一筹。只是细节的处理有点变化,也正因为这点变化,代码的编写更加困难,结构更加散乱。因而,从代码上讲,本代码比Recursion逊色不少。也就是因为这段代码,才最终将测试用的XML文档“修修补补”,成为那个让人看了头疼的代码:-)代码实在没劲,所以,就没有做注释,各位看客要能看懂更好,看不懂就算了,第4篇将以Recursion为基础,重写代码。 JAVASCRIPT实现: <script language="javascript"> /* *************************************************** *代码:Qr http://Qr.blogger.org.cn *时间:2007/07/15 21:58:00 *功能:遍历XML的所有节点、属性,仿IE浏览XML方式输出 *类型:支持TEXT、CDATA、COMMENT、ELEMENT节点 *************************************************** */ var xml = null;format = "",fo = " "; 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) { do{ if(o.nodeType!=3 && o.nodeType!=4 && o.nodeType!=8){ if(o!=xml.documentElement)format += fo; document.write(format+"<"+o.nodeName); attrparser(o); if(o.nextSibling && !o.firstChild){ document.write("/>"+"<br>"); format = format.substr(0,format.length-24); o = o.nextSibling; continue; } if(!o.nextSibling && !o.firstChild){ document.write("/>"+"<br>"); if(o==xml.documentElement)break; format = format.substr(0,format.length-24); do{ if(o==xml.documentElement || o.nextSibling)break; if(o.parentNode){ document.write(format); document.write("</"+o.parentNode.nodeName+">"+"<br>"); format = format.substr(0,format.length-24); o = o.parentNode; } }while(o!=xml.documentElement && !o.nextSibling); if(o==xml.documentElement)break; o = o.nextSibling; continue; }else{ document.write(">"); if(o.firstChild.nodeType!=3)document.write("<br>"); } } if(o.nodeType==4){ format += fo; document.write(format+"<![CDATA["+o.nodeValue+"]]>"+"<br>"); format = format.substr(0,format.length-24); if(!o.parentNode.nextSibling && o.parentNode.lastChild==o && o.parentNode.firstChild!=o){ document.write(format+"</"+o.parentNode.nodeName+">"+"<br>"); format = format.substr(0,format.length-24); } if((!o.nextSibling && !o.previousSibling) || (!o.nextSibling && o.parentNode.nextSibling)){ document.write(format+"</"+o.parentNode.nodeName+">"+"<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+"</"+o.nodeName+">"+"<br>"); break; }else{continue;} } if(o.nodeType==8){ format += fo; document.write(format+"<!--"+o.nodeValue+"-->"+"<br>"); format = format.substr(0,format.length-24); if(!o.parentNode.nextSibling && o.parentNode.lastChild==o && o.parentNode.firstChild!=o){ document.write(format+"</"+o.parentNode.nodeName+">"+"<br>"); format = format.substr(0,format.length-24); } if((!o.nextSibling && !o.previousSibling) || (!o.nextSibling && o.parentNode.nextSibling)){ document.write(format+"</"+o.parentNode.nodeName+">"+"<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+"</"+o.nodeName+">"+"<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; }while(o!=xml.documentElement); } function CC2E(oo){ do{ 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("</"+oo.parentNode.nodeName+">"+"<br>"); format = format.substr(0,format.length-24); oo = oo.parentNode; } }while(oo!=xml.documentElement && !oo.nextSibling); return oo; } function N2PN(np){ do{ if(np==xml.documentElement || np.nextSibling)break; if(np.parentNode)np = np.parentNode; }while(np!=xml.documentElement && !np.nextSibling); 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> 测试用的XML看 相关文章:通过javascript+DOM方式遍历XML节点(一)[原创]
Posted by Qr on 2007/7/27 11:00:00
发表评论: |