Estoy usando xmlbuilder para generar un archivo xml de forma dinámica a partir de los datos de una consulta. Para el nodo de atributo personalizado retailTypeId, necesito recorrer una matriz de retailTypeIds y agregar un nodo de valor para cada uno en la matriz.
Todo lo que hago para inyectar un bucle for para determinar qué valores se necesitan para cada nuevo nodo de tienda rompe el código. En este momento estoy insertando el primero en una posible cadena de valores separados por el punto y coma. Lo he intentado todo lo que se me ocurre y no puedo hacerlo funcionar. ¿Alguien puede ayudar?
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()cree un fragmento XML con bucle for y luego agréguelo al XML principal:
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});