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

159
Vistas
xml2js usage to build up nodes dynamically based on queried data - need <value>some data </value> zero to many times

I'm using xmlbuilder to generate an xml file dynamically from data in a query. For the retailerTypeId custom-attribute node I need to loop through an array of retailerTypeIds and add a value node for each one in the array.

Everything I do to inject a for loop to determine what values are needed for each new store node breaks the code. Right now I'm inserting the first one in a possible string of values separated by the semi-colon. I've tried everyway I can think of an can't get it to work. Can anyone help?

xml.att('xmlns', 'http://www.demandware.com/xml/impex/store/2007-04-30')
   .ele('store', {'store-id':  storeRecord.ID})
   .ele('name', storeRecord.LOCATION_NAME__C).up()
   .ele('custom-attributes')
      .ele('custom-attribute', { 'attribute-id': 'retailerTypeId', 'xml:lang': 'x-default' }) 
         .ele('value', storeRecord.RETAILER_TYPE__C.split(';')[0]).up()
      .up()
   .up()
.up()
xml.end({ pretty: true});`

I was attempting something like this but don't have the syntax correct as it closes the custom-attribute node:

    .ele('custom-attribute', { 'attribute-id': 'retailerTypeId','xml:lang' : 'x-default' })
        for (var i = 0; i < retailerTypes.length; i++) {
            xml.ele('value').txt(retailerTypes[i])
            .up()
        }
    .up()
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

create an XML fragment with for loop, and then add it to the main XML:

xml.att('xmlns', 'http://www.demandware.com/xml/impex/store/2007-04-30')
   .ele('store', {'store-id':  storeRecord.ID})
   .ele('name', storeRecord.LOCATION_NAME__C).up()

const custom = xmlbuilder.create('custom-attributes');

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

      custom
      .ele('custom-attribute', { 'attribute-id': 'retailerTypeId', 'xml:lang': 'x-default' }) 
      .ele('value', retailerTypes[i]).up().up()
             
    }

xml.importDocument(custom);

xml.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