通过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 = " ";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+"<"+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); for(;;){ 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; } } 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; }} 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("</"+oo.parentNode.nodeName+">"+"<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+"</"+oo.nodeName+">"+"<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节点(二)[原创]
Posted by Qr on 2007/7/27 13:02:50
发表评论: |