hey guys I have trouble with loop string for xml inside for loop. I have tried this and the loop is not work
var data = [{"name": "Tom", age: "20"}, {"name": "Jerry", age: "20"}]
for (i = 0; i < data.length; i++) {
var xmltext = '<Placemark>\n' +
'\t<name>' + data[i].name + '</name>\n' +
'\t<age>' + data[i].age + '</age>\n' +
'</Placemark>';
}
var filename = "file.xml";
var bb = new Blob([xmltext], {type: 'text/xml'});
document.getElementById('download').setAttribute('href', window.URL.createObjectURL(bb));
document.getElementById('download').setAttribute('download', filename);
the result I expected in xml
<Placemark>
<name>Tom</name>
<Age>20</Age>
</Placemark>
<Placemark>
<name>Jerry</name>
<Age>20</Age>
</Placemark>
is there something i'm missing or i'm doing it wrong? I wish your help
you create the on every iteration the xmltext var new. But you want to just declerate it once and add the String to it like this:
var data = [{"name": "Tom", age: "20"}, {"name": "Jerry", age: "20"}]
var xmltext = "";
for (i = 0; i < data.length; i++) {
xmltext = xmltext + '<Placemark>\n' +
'\t<name>' + data[i].name + '</name>\n' +
'\t<age>' + data[i].age + '</age>\n' +
'</Placemark>';
}