0

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()

1 Answers1

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});
traynor
  • 5,490
  • 3
  • 13
  • 23
  • Oh that worked perfectly! I hadn't seen anything about that importDocument in searching for a solution, I was looking for append type functionality. Thank you so much for your response. – Jennifer Mar 31 '22 at 12:43