Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
Loop in nodejs xmlbuilder

How can I loop through and choose some data from the array? I try to manage it, but inside the loop the xmlbuilder did not define the expected result. To choose the right value maybe it is easy just I have to define the exact element but not yet tested,

Expected result:

<?xml version="1.0" encoding="utf-8"?>
<export>
  <sor>
    <ragszam>PBPU8393290190011113017025</ragszam>
    <ugyfeladat1>10093-669800</ugyfeladat1>
  </sor>
  <sor>
    <ragszam>PBPU83932901903213117025</ragszam>
    <ugyfeladat1>10093-885200</ugyfeladat1>
  </sor>
  <sor>
    <ragszam>PBPU83616165165113017025</ragszam>
    <ugyfeladat1>10093-998700</ugyfeladat1>
  </sor>
</export>``` 

My Code:

var builder = require('xmlbuilder');

var array = [
  {
    'CsoportID': 'CLG22030934458',
    'Shipment ID': 'PBPU8393290190011113017025',
    'Requested': '2022-03-09 11:17:05',
    'Status': 'Folyamatban',
    'Futár felvette': '2022-03-09 17:27:47',
    'Követőkód': '10093-669800'
  },
  {
    'CsoportID': 'CLG22030934458',
    'Shipment ID': 'PBPU83932901903213117025',
    'Requested': '2022-03-09 11:17:05',
    'Status': 'Folyamatban',
    'Futár felvette': '2022-03-09 17:27:47',
    'Követőkód': '10093-885200'
  },
  {
    'CsoportID': 'CLG22030934458',
    'Shipment ID': 'PBPU83616165165113017025',
    'Requested': '2022-03-09 11:17:05',
    'Status': 'Folyamatban',
    'Futár felvette': '2022-03-09 17:27:47',
    'Követőkód': '10093-998700'
  }
]


for (let i = 0; i < array.length; i++) {

  var feed = builder.create('export', { encoding: 'utf-8' })
    .ele('sor')
      .ele('ragszam', array[i][1]).up()
      .ele('ugyfeladat1', array[i][5]).up()
      .up()
  }

console.log(feed.end({ pretty: true }));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

create the root element outside the loop and add new child elements to it in the loop, also access desired object properties by their name, not index

try this:

var builder = require('xmlbuilder');

var array = [
  {
    'CsoportID': 'CLG22030934458',
    'Shipment ID': 'PBPU8393290190011113017025',
    'Requested': '2022-03-09 11:17:05',
    'Status': 'Folyamatban',
    'Futár felvette': '2022-03-09 17:27:47',
    'Követőkód': '10093-669800'
  },
  {
    'CsoportID': 'CLG22030934458',
    'Shipment ID': 'PBPU83932901903213117025',
    'Requested': '2022-03-09 11:17:05',
    'Status': 'Folyamatban',
    'Futár felvette': '2022-03-09 17:27:47',
    'Követőkód': '10093-885200'
  },
  {
    'CsoportID': 'CLG22030934458',
    'Shipment ID': 'PBPU83616165165113017025',
    'Requested': '2022-03-09 11:17:05',
    'Status': 'Folyamatban',
    'Futár felvette': '2022-03-09 17:27:47',
    'Követőkód': '10093-998700'
  }
]


var feed = builder.create('export', { encoding: 'utf-8' })

for (let i = 0; i < array.length; i++) {

    feed.ele('sor')
      .ele('ragszam', array[i]['Shipment ID']).up()
      .ele('ugyfeladat1', array[i]['Követőkód']).up()
      .up()
  }

console.log(feed.end({ pretty: true }));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda