Converting a table into XML then reverting into XML using Javascript/jQuery



I need to convert a hard-coded HTML table into an XML. I am able to convert the HTML table into XML for which the coded is shown below. Now, I need to convert back that same XML file into HTML table in the same program. Can anyone please help me out with that. Thanks


Code is:



<body>
<!--Table begins-->
<table id="tb" border="1px" cellpadding="5px" cellspacing="5px">
<tr>
<th>Name</th>
<th>Company</th>
<th>Country</th>
<th>First Name</th>
<th>Last Name</th>
</tr>

<tr>
<td>ABC</td>
<td>DEF</td>
<td>GHI</td>
<td>JKL</td>
<td>MNO</td>
</tr>

<tr>
<td>PQR</td>
<td>STU</td>
<td>VWX</td>
<td>AGC</td>
<td>FSW</td>

</tr>
</table>
<!--Table ends here-->

<script>
$(function(){
var xml = "";
xml+= '<?xml version= "1.0" encoding="UTF-8"?>';
xml+= '<root>';

$('tr:not(:first)').each(function(j, tr)
{
$tr = $(tr);
xml += '<comp>';

var index1 = $.trim($tr.find('td:first').text());
xml += '<name>'+index1+'';
xml += '</name>';

var index2 = $.trim($tr.find('td:nth-child(2)').text());
xml += '<company>'+index2+'';
xml += '</company>';

var index3 = $.trim($tr.find('td:nth-child(3)').text());
xml += '<country>'+index3+'';
xml += '</country>';

var index4 = $.trim($tr.find('td:nth-child(4)').text());
xml += '<fname>'+index4+'';
xml += '</fname>';

var index5 = $.trim($tr.find('td:last').text());
xml += '<lname>'+index5+'';
xml += '</lname>';

xml += '</comp>';
});
xml+= '</root>';

console.log(xml);
});
</script>
</body>
</html>

No comments:

Post a Comment