如何将数据添加到节点js中的文件

时间:2016-08-04 13:30:55

标签: xml node.js tags rss

我希望我的xml文件是这样的

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss>
  <channel>
    <title>xpertdox</title>
    <link>http://localhost:4000</link>
    <description>Find an xpert doctor for a rare disease</description>
          <item>
             <title>hi</title>
             <link>https://www.hi.com</link>
         </item>

         <item>
              <title>hello</title>
              <link>https://www.hello.com</link>
            </item>

  </channel>
</rss>

但我这样做了,

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss>
  <channel>
    <title>xpertdox</title>
    <link>http://localhost:4000</link>
    <description>Find an xpert doctor for a rare disease</description>
    <item>
      <title>hi</title>
      <link>https://www.xpertdox.com/blog/blog-description/experts-personalized-med</link>
    </item>
  </channel>
</rss><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss>
  <channel>
    <title>xpertdox</title>
    <link>http://localhost:4000</link>
    <description>Find an xpert doctor for a rare disease</description>
    <item>
      <title>JJJJJJJ@@@</title>
      <link>https://www.xpertdox.com/blog/blog-description/experts-personalized-med</link>
    </item>
  </channel>
</rss>

这里的rss,xml标签因为以下代码而重复,

 var blog = {blog_id:"experts-personalized-med",title:"JJJJJJJ@@@"}
    var obj = {rss:{channel:{title:"xpertdox",link:"http://localhost:4000",description:"Find an xpert doctor for a rare disease",item:{title: blog.title, link: "https://www.xpertdox.com/blog/blog-description/"+blog.blog_id}}}};
            var fs = require('fs');
            var xml2js = require('xml2js');
            var builder = new xml2js.Builder();
            var xml = builder.buildObject(obj);
            console.log(xml);  
                fs.appendFile('public/feed.xml', xml, function (err){
                if (err) throw err;
                    console.log('It\'s saved!');
                }); 

在我的代码中,无论何时输入新对象,都会重复标记。这是我的情况,任何人都可以帮助我获得所需的形状

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <rss>
      <channel>
        <title>xpertdox</title>
        <link>http://localhost:4000</link>
        <description>Find an xpert doctor for a rare disease</description>
           ------------------------------------------
      </channel>
    </rss>

这是我的文件初始化的方式,当我追加它时,新对象应介于两者之间。

这在我的控制台中显示

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss>
  <channel>
    <title>xpertdox</title>
    <link>http://localhost:4000</link>
    <description>Find an xpert doctor for a rare disease</description>
    <item>
      <title>JJJJJJJ@@@</title>
      <link>https://www.xpertdox.com/blog/blog-description/experts-personalized-med</link>
    </item>
  </channel>
</rss>

插入2条记录后,这是我的文件

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss>
  <channel>
    <title>xpertdox</title>
    <link>http://localhost:4000</link>
    <description>Find an xpert doctor for a rare disease</description>
    <item>
      <title>JJJJJJJ@@@</title>
      <link>https://www.xpertdox.com/blog/blog-description/experts-personalized-med</link>
    </item>
  </channel>
</rss><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss>
  <channel>
    <title>xpertdox</title>
    <link>http://localhost:4000</link>
    <description>Find an xpert doctor for a rare disease</description>
    <item>
      <title>JJJJJJJ@@@</title>
      <link>https://www.xpertdox.com/blog/blog-description/experts-personalized-med</link>
    </item>
  </channel>
</rss><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss>
  <channel>
    <title>xpertdox</title>
    <link>http://localhost:4000</link>
    <description>Find an xpert doctor for a rare disease</description>
    <item>
      <title>JJJJJJJ@@@</title>
      <link>https://www.xpertdox.com/blog/blog-description/experts-personalized-med</link>
    </item>
  </channel>
</rss>

0 个答案:

没有答案