JavaScript converts xml into an html table, xmltable

Source: Internet
Author: User

JavaScript converts xml into an html table, xmltable

This example describes how to convert xml into an html table using JavaScript. Share it with you for your reference. The details are as follows:

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

The following is a simple example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

I hope this article will help you design javascript programs.

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.