The JavaScript implementation transforms the XML into an HTML table

Source: Internet
Author: User
Tags rowcount

JavaScript implements the method of converting XML into an HTML table table. function ConvertToTable (targetnode) { //If the TargetNode is XmlNode this line must be removed  //i couldnt f IND A-to-parse XML string to XML node  //-I parse XML string to XML document  targetnode = Targetnode.chi Ldnodes[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 = t Argetnode.childnodes[0].childnodes[i]. nodeName;  }  //now fill the rows with Data  for (Var i2=0;i2<rowcount;i2++)  {  var newRow = mytable.i Nsertrow ();    for (var j=0;j<columncount;j++)    {   var Newcell = Newrow.insertcell ();   &nbs p;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 ">

{

 //if the TargetNode is XmlNode this line must be removed  //i couldnt find a-to-parse XML string to XML n Ode  //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 = t Argetnode.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++) & nbsp  {   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 (); &nbs P Xmldoc=parser.parsefromstring (text, "Text/xml");   Return xmldoc;  }   catch (e)   {  alert (e.message),   return,  }  }} var myxml = ' &LT;TABLENAME&G T \   <firstRow> \   <field1>1</field1> \ &nbsP <field2>2</field2> \   </firstRow> \   <firstRow> \   <field1>3</field1 > \   <field2>4</field2> \   </firstRow> \  </TableName> ';  var MyDoc = loadxmldocfromstring (myXML); document.write (ConvertToTable (MYDOC)); </script> </body>

The original codego.net JavaScript implementation transforms XML into HTML table

The JavaScript implementation transforms the XML into an HTML table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.