Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda