XML2JS使用基于查询的数据动态构建节点 - 需要<value>一些数据</value>零到多次



我使用xmlbuilder从查询中的数据动态生成一个xml文件。对于retailerTypeId自定义属性节点,我需要循环遍历retailerTypeId的数组,并为数组中的每个添加一个值节点。

我为注入for循环以确定每个新存储节点需要什么值所做的一切都会破坏代码。现在,我将第一个插入到一个可能的值字符串中,该字符串由分号分隔。我想尽了一切办法,但还是没能成功。有人能帮忙吗?

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

使用for循环创建一个XML片段,然后将其添加到主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});

最新更新