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

195
Views
La mitad del período me pide que genere un conteo para 'país' y un conteo para 'título de contacto' del documento XML

Creé una tabla que llena los datos del archivo XML. Pero tengo problemas para "crear un recuento". Solo quiero contar los países y los títulos de trabajo, hay muchos que se repiten. Creo que el Prof. quiere que enumeremos algo como:

Alemania: 4 cuentas

México: 5 unidades

Fragmento de documento XML:

 <dataroot> <forMid> <cust>ALFKI</cust> <abc>Maria Anders</abc> <contacttitle>Sales Representative</contacttitle> <city>Berlin</city> <country>Germany</country> </forMid> <forMid> <cust>ANATR</cust> <abc>Ana Trujillo</abc> <contacttitle>Owner</contacttitle> <city>México DF</city> <country>Mexico</country> </forMid> <forMid> <cust>ANTON</cust> <abc>Antonio Moreno</abc> <contacttitle>Owner</contacttitle> <city>México DF</city> <country>Mexico</country> </forMid> </dataroot>

Así es como se ve mi secuencia de comandos de tabla si ayuda:

 function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { imAnnoyed(this); } }; xhttp.open("GET", "midA.xml", true); xhttp.send(); } function imAnnoyed(xml) { var i; xmlDoc = xml.responseXML; const mad = xmlDoc.getElementsByTagName("forMid"); var table = "<col><col><col><col><col><tr><th>Business ID</th><th>Customer Name</th><th>Title</th><th>City</th><th>Country</th></tr>"; for (i = 0; i < mad.length; i++) { table += "<tr><td>" + mad[i].getElementsByTagName("cust")[0].childNodes[0].nodeValue + "</td><td>" + mad[i].getElementsByTagName("abc")[0].childNodes[0].nodeValue + "</td><td>" + mad[i].getElementsByTagName("contacttitle")[0].childNodes[0].nodeValue + "</td><td>" + mad[i].getElementsByTagName("city")[0].childNodes[0].nodeValue + "</td><td>" + mad[i].getElementsByTagName("country")[0].childNodes[0].nodeValue + "</td><tr>"; } document.getElementById("table").innerHTML = table; }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sugeriría no usar ajax, usar fetch (integrado en js)

  1. Obtenga todas las etiquetas con el nombre de etiqueta que desee
  2. Obtener HTML/contenido interno de la etiqueta.
  3. Crear mapa hash. la clave será país país.
  4. Establezca el conteo en 1 si es un elemento nuevo, si ya existe, incremente en uno.
  5. Obtenga el conteo recorriendo el hash.
 function loadDoc() { // use fetch to load xml file fetch("midA.xml") // convert response to text .then((resp) => resp.text()) // parse text to xml .then((xml) => new window.DOMParser().parseFromString(xml, "text/xml")) // call your function imAnnoyed .then(imAnnoyed) // catch errors .catch((e) => console.log(e)); } function imAnnoyed(xml) { // select all tags country let countryList = xml.querySelectorAll("country"); // convert tag to innerHTML (text) countryList = Array.from(countryList).map((tag) => tag.innerHTML); // create hashmap const hash = new Map(); // countryList.forEach((country) => // if country is in hashmap increment value by 1 // else add it and set value to 1 hash.has(country) ? hash.set(country, hash.get(country) + 1) : hash.set(country, 1) ); // convert to array items = Array.from(hash.entries()); // loop through final result items.forEach(([country, count]) => { // print values or add your logic below console.log(country, count); }); } // Output: // Germany 1 // Mexico 2
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!