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.