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

204
Views
Javascript: cambie de importar datos desde un archivo XML a importar datos desde una matriz de objetos JS

Necesito editar un script para pasar de leer datos XML de un archivo xml a leer datos de una matriz de objetos JS.

supongamos que el archivo xml es x.xml :

 <xml> <location> <name>cafe 1</name> <address>1 cafe st</address> </location> <location> <name>cafe 2</name> <address>2 cafe st</address> </location> </xml>

El siguiente código llena una matriz con datos de un archivo xml

 $.ajax({ type: "GET", url: "x.xml", dataType: "xml", success: function(xml) { $(xml).find('location').each(function(){ i +=1; var name = $(this).find('name').text(); var address = $(this).find('address').text(); table[i] = {name:name, address:address}; });

..puedo reescribir esa salida como

 var table = [ {"name":"cafe 1", "address":"1 cafe st"}, {"name":"cafe 2", "address":"2 cafe st"}, ]

... y llame a los datos en la matriz usando

 var m; for ( m = 1; m < table.length-1; m++) { if (table[m].name == "cafe 1" ....

Intentando que funcione lo más cerca posible de esta sintaxis. Solo quiero obtener los valores en la matriz en el formato que se muestra

 $(xml).find('location').each(function() { i += 1; var name = $(this).find('name').text(); var address = $(this).find('address').text(); table[i] = {name:name, address:address}; });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Básicamente lo tienes, aunque simplifiqué un poco tu código aquí

 let xml = `<xml> <location> <name>cafe 1</name> <address>1 cafe st</address> </location> <location> <name>cafe 2</name> <address>2 cafe st</address> </location> </xml>` let table = [] $(xml).find('location').each(function(i) { // here's one way, using array.push() /* table.push({ name: $(this).find('name').text(), address: $(this).find('address').text() }); */ // heres another way, using a specific index // i (the index) is supplied by the $.each function table[i] = { name: $(this).find('name').text(), address: $(this).find('address').text() } }); table.forEach(row => { $('#theTable').append(`<tr><td>${row.name}</td><td>${row.address}</td></tr>`) })
 #theTable td { padding:5px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id='theTable' border='1'><tr><td>Name</td><td>Address</td></tr></table>

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!