I have a XML file and I'm populating a HTML table with the data in the XML using jQuery. The XML file has the following format:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row>
<Patologia>Alzheimer</Patologia>
<Molecula>Galantamina</Molecula>
<Nombre_Comercial>RAZADYNE®</Nombre_Comercial>
<Fecha_Exp>2017-06</Fecha_Exp>
<Propietario>JANSSEN PHARMS</Propietario>
</Row>
<Row>
<Patologia>Alzheimer</Patologia>
<Molecula>Liraglutida</Molecula>
<Nombre_Comercial>Victoza®</Nombre_Comercial>
<Propietario>NOVO NORDISK INC</Propietario>
</Row>
<Row>
<Patologia>Alzheimer</Patologia>
<Nombre_Comercial>ARICEPT ODT®</Nombre_Comercial>
<Fecha_Exp>2018-03</Fecha_Exp>
<Propietario>EISAI INC</Propietario>
</Row>
I have the following JavaScript:
function showData() {
$.ajax({
type: "GET",
url: "test2.xml",
dataType: "xml",
success: function (xml) {
$('#mainTable').empty();
var fila = $(xml).find("Row");
var tabla = $("#mainTable");
var row = $(tabla[0].insertRow(-1));
fila.eq(0).children().each(function () {
var headerCell = $("<th />");
headerCell.html(this.nodeName);
row.append(headerCell);
});
$(fila).each(function () {
row = $(tabla[0].insertRow(-1));
$(this).children().each(function () {
var cell = $("<td />");
cell.html($(this).text());
row.append(cell);
});
});
var thead = tabla.find("thead");
var thRows = tabla.find("tr:has(th)");
thead = $("<thead />").prependTo(tabla);
var copy = thRows.clone(true).appendTo("thead");
thRows.remove();
$("#mainTable td:empty").append("epale");
}
});
}
The problem that I'm having is that in the XML file some rows are missing a column or two. When filling the table using the jQuery code it messes up the columns when a column is missing from the XML. How can I add an empty cell when a column is missing from the xml?
I hope I was able to explain the issue.
Any help would be greatly appreciated.