javascriptnode.jsxml-builder

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 }));

Solution

  • 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 }));