最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论