![JavaScript实现将xml转换成html table表格办法【javascript】](https://700zi.400zi.cn/uploadfile/img/14/552/af7423b0d22797dc7ce120959645a7ab.jpg)
web前端|js教程
JavaScript,xml,转换,table表格
web前端-js教程
本文实例讲述了JavaScript实现将xml转换成html table表格的方法。分享给大家供大家参考。具体如下:
首页视频源码,ubuntu保留文件安装,爬虫毕设ppt,php单干,vue seo方案lzw
function ConvertToTable(targetNode){ // if the targetNode is xmlNode this line must be removed // i couldnt find a way to parse xml string to xml node // so i parse xml string to xml document targetNode = targetNode.childNodes[0]; // first we need to create headers var columnCount = targetNode.childNodes[0].childNodes.length; var rowCount = targetNode.childNodes.length // name for the table var myTable = document.createElement("table"); myTable.border = 1; myTable.borderColor ="green"; var firstRow = myTable.insertRow(); var firstCell = firstRow.insertCell(); firstCell.colSpan = columnCount; firstCell.innerHTML = targetNode.nodeName; // name for the columns var secondRow = myTable.insertRow(); for(var i=0;i<columnCount;i++) { var newCell = secondRow.insertCell(); newCell.innerHTML = targetNode.childNodes[0].childNodes[i].nodeName; } // now fill the rows with data for(var i2=0;i2<rowCount;i2++) { var newRow = myTable.insertRow(); for(var j=0;j<columnCount;j++) { var newCell = newRow.insertCell(); newCell.innerHTML = targetNode.childNodes[i2].childNodes[j].firstChild.nodeValue; } } // i prefer to send it as string instead of a table object return myTable.outerHTML;}
下面是一个简单的示例用法:
ex ui 源码,华硕 ubuntu安装,tomcat启动后就停止,爬虫软件cms,2300套php网站源码,seo从0到1怎么做lzw
Untitled Pagefunction ConvertToTable(targetNode){ // if the targetNode is xmlNode this line must be removed // i couldnt find a way to parse xml string to xml node // so i parse xml string to xml document targetNode = targetNode.childNodes[0]; // first we need to create headers var columnCount = targetNode.childNodes[0].childNodes.length; var rowCount = targetNode.childNodes.length // name for the table var myTable = document.createElement("table"); myTable.border = 1; myTable.borderColor ="green"; var firstRow = myTable.insertRow(); var firstCell = firstRow.insertCell(); firstCell.colSpan = columnCount; firstCell.innerHTML = targetNode.nodeName; // name for the columns var secondRow = myTable.insertRow(); for(var i=0;i<columnCount;i++) { var newCell = secondRow.insertCell(); newCell.innerHTML = targetNode.childNodes[0].childNodes[i].nodeName; } // now fill the rows with data for(var i2=0;i2<rowCount;i2++) { var newRow = myTable.insertRow(); for(var j=0;j<columnCount;j++) { var newCell = newRow.insertCell(); newCell.innerHTML = targetNode.childNodes[i2].childNodes[j].firstChild.nodeValue; } } // i prefer to send it as string instead of a table object return myTable.outerHTML;}function loadXmlDocFromString(text){ try //Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.loadXML(text); return xmlDoc; } catch(e) { try // Firefox, Mozilla, Opera, etc. { parser=new DOMParser(); xmlDoc=parser.parseFromString(text,"text/xml"); return xmlDoc; } catch(e) { alert(e.message); return; } }}var myXml = \ \ 1 \ 2 \ \ \ 3 \ 4 \ \ ; var myDoc = loadXmlDocFromString(myXml);document.write( ConvertToTable(myDoc));
天天动听安卓源码,vscode自动改名,ubuntu librt,tomcat调端口,sqlite3删视图,DZ第三方支付插件,常见的前端开发框架和技术,农村自建房爬虫太多,php 数组顺序,印江县seo优化,在线相册网站模板下载工具,前端聊天网页源码,帝国建站模板lzw