国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript實現將xml轉換成html table表格的方法

2019-11-20 12:38:43
字體:
來源:轉載
供稿:網友

本文實例講述了JavaScript實現將xml轉換成html table表格的方法。分享給大家供大家參考。具體如下:

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;}

下面是一個簡單的示例用法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>Untitled Page</title></head><body><script type="text/javascript">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;}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 = '<TableName> /  <firstRow> /  <field1>1</field1> /  <field2>2</field2> /  </firstRow> /  <firstRow> /  <field1>3</field1> /  <field2>4</field2> /  </firstRow> / </TableName>'; var myDoc = loadXmlDocFromString(myXml);document.write( ConvertToTable(myDoc));</script></body></html>

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 城固县| 临夏县| 河间市| 萨嘎县| 辽阳县| 玛纳斯县| 武义县| 南充市| 西充县| 五家渠市| 永丰县| 都昌县| 晋宁县| 柞水县| 治多县| 班玛县| 开阳县| 沐川县| 迁西县| 井研县| 福建省| 祁连县| 德化县| 兴仁县| 安平县| 罗山县| 寻甸| 正镶白旗| 光山县| 金坛市| 天长市| 彩票| 台北县| 宝兴县| 永和县| 会昌县| 琼结县| 高淳县| 聂拉木县| 波密县| 镇江市|