Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
javascript, loop string xml inside for loop

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>';
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!