我的xml文件

   <?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
      <title>XpertDox Home Page</title>
  <link>http://www.xpertdox.com</link>
    <description>Find a doctor</description>
<item>
<title>Xpertdox</title>
<link>http://www.xpertdox.com/rss.xml</link>
<description>Find an Xpert Doctor for an unknown Disease</description>
</item>

  I want  to  add this
    <item>
<title>Tony</title>
<link>startk</link>
<description>hi</description>
</item>



    

我的node.js

var obj = {title: "Tony", link: "Stark" , "description":"hi"};
var fs = require('fs');
var xml2js = require('xml2js');

var builder = new xml2js.Builder();
var xml = builder.buildObject(obj);

fs.writeFile('feed.xml', xml, function (err){
if (err) throw err;
    console.log('It\'s saved!');
});


我想替换我的obj中声明的标题标签和链接,但这不起作用。任何人都可以更改我的代码........

最佳答案

在Google上发布此帖子时,我正在寻找稍微不同的东西,但是在这里,我的这段代码应该为遇到此帖子的其他人解决此问题。

function xmlFileToObject(path) {
    return new Promise((resolve,reject) => {
        getFile(path).then(xml => {
            parseXml(xml).then(result => {
                resolve(result);
            });
        });
    });
}
function getFile(path) {
    return new Promise((resolve, reject) => {
        fs.readFile(path, 'utf8', function (err,data) {
            if (err) console.error(err);
            resolve(data);
        });
    });
}
function parseXml(xml) {
    return new Promise((resolve,reject) => {
        var parseString = require('xml2js').parseString;
        parseString(xml, function (err, result) {
            resolve(result);
        });
    });
}

09-06 15:33