JavaScript implements the method of converting XML into HTML table tables _javascript tips

Source: Internet
Author: User
Tags rowcount

This article illustrates the JavaScript implementation method of translating XML into an HTML table table. Share to everyone for your reference. as follows:

function ConvertToTable (TargetNode) {//If the TargetNode is XmlNode this line must to 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];
 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 the a Table object return mytable.outerhtml; }

The following is a simple example usage:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

The

wants this article to help you with your JavaScript programming.

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.