I'm using xml2js to build a .xml from my registers. For each element, i need to set the key with same name, example: key: product with an attribute id
The expected result:
<products>
<product id="H12896">
<name>Grand Hotel New York</name>
<price>120.80</price>
</product>
<product id="...">
...
</product>
</products>
My code:
var products = [];
_.each(list, function(element) {
var obj = {
name: element.title,
price: element.price,
};
products.push(obj);
});
var builder = new xml2js.Builder({rootName: 'products'});
var xml = builder.buildObject(products);
fs.writeFile('pacotes.xml', xml, (err) => {
if (err) throw (err);
});
Output result:
<products>
<0>
<name>Mountain Do</name>
<price>0</price>
</0>
</products>
I checked the documentation, but nothing yet. Thanks
I'm using xml2js to build a .xml from my registers. For each element, i need to set the key with same name, example: key: product with an attribute id
The expected result:
<products>
<product id="H12896">
<name>Grand Hotel New York</name>
<price>120.80</price>
</product>
<product id="...">
...
</product>
</products>
My code:
var products = [];
_.each(list, function(element) {
var obj = {
name: element.title,
price: element.price,
};
products.push(obj);
});
var builder = new xml2js.Builder({rootName: 'products'});
var xml = builder.buildObject(products);
fs.writeFile('pacotes.xml', xml, (err) => {
if (err) throw (err);
});
Output result:
<products>
<0>
<name>Mountain Do</name>
<price>0</price>
</0>
</products>
I checked the documentation, but nothing yet. Thanks
Share Improve this question asked Mar 9, 2016 at 20:16 victorkurauchivictorkurauchi 1,40919 silver badges23 bronze badges2 Answers
Reset to default 6Is there a reason you're not using XMLBuilder? https://github./oozcitak/xmlbuilder-js/
XMLBuilder seems much better suited for what you're wanting to do, and is much, much more popular (ex: 4 million downloads in the last month). xml2js is better suited for reading in JavaScript, but XMLBuilder is definitely what you'd want to use.
And if I'm reading correctly... xml2js is just building on XMLBuilder anyway.
var builder = require('xmlbuilder'),
xml = builder.create('root'),
products = xml.ele('products'),
product;
_.each(list, function(element) {
product = products.ele('product');
product.att('id', element.id);
product.ele('name', null, element.name);
product.ele('price', null, element.price);
});
xml = xml.end();
fs.writeFile('pacotes.xml', xml, (err) => {
if (err) throw (err);
});
You can just create a list of objects and use the name that you want to use as the key:
const productList = [
{ name: 'foo', price: 30 },
{ name: 'bar', price: 5 },
{ name: 'baz', price: 87 },
];
const obj = { product: [] }
productList.forEach(element => obj.product.push({
name: element.name,
price: element.price,
}));
const builder = new xml2js.Builder({rootName: 'products'});
const xml = builder.buildObject(obj);
console.log(xml);
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<products>
<product>
<name/>
<price>30</price>
</product>
<product>
<name/>
<price>5</price>
</product>
<product>
<name/>
<price>87</price>
</product>
</products>