I’m trying to achieve this XML output in NodeJS in a Lambda function :
<Section ProductId="46m8t">
<Item ItemQty="1">
<Image>Road.jpg</Image>
<ItemProductValue>0.50</ItemProductValue>
<ItemShippingValue>1.00</ItemShippingValue>
</Item>
</Section>
from this :
Sections: listedObjects.Contents.map(file => ({
Section: {
$: { ProductId: "46m8t" },
Item: {
$: {
ItemQty: "1",
Image: file.Key.split('/').pop(),
ItemProductValue: "0.50",
ItemShippingValue: "1.00"
}
}
}
}))
Using this builder setting :
const builder = new xml2js.Builder({
headless: true,
renderOpts: { 'pretty': true },
explicitArray: true,
explicitChildren: true,
preserveChildrenOrder: true
});
The function just drops as the XML build fails.
This works :
Sections: listedObjects.Contents.map((file) => ({
Section: {
$: { ProductId: "46m8t" }, // Included ProductId directly in the Section tag
Item: {
ItemQty: "1",
Image: file.Key.split('/').pop(), // Only the filename
ItemProductValue: "0.50",
ItemShippingValue: "1.00"
}
}
}))
but gives me this output :
<Sections>
<Section ProductId="46m8t">
<Item>
<ItemQty>1</ItemQty>
<Image>Road.jpg</Image>
<ItemProductValue>0.50</ItemProductValue>
<ItemShippingValue>1.00</ItemShippingValue>
</Item>
</Section>
</Sections>
Using this Builder :
const builder = new xml2js.Builder({ headless: true, renderOpts: { 'pretty': true } });
New contributor
Patrick Collet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.