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 = ' <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