Hey everyone, I'm having some trouble with looping through strings for XML inside a for loop. I've attempted the following code but the loop doesn't seem to be working:
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);
I'm looking for the following XML result:
<Placemark>
<name>Tom</name>
<Age>20</Age>
</Placemark>
<Placemark>
<name>Jerry</name>
<Age>20</Age>
</Placemark>
Am I missing something or doing something wrong? Any help would be greatly appreciated.