javascript - Using xml2js to build xml with equal child key? - Stack Overflow

I'm using xml2js to build a .xml from my registers. For each element, i need to set the key with s

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

Is 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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745048086a4608218.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信