【问题标题】:I need a way to take some json and convert it to xml using node.js我需要一种方法来获取一些 json 并使用 node.js 将其转换为 xml
【发布时间】:2012-01-13 03:31:23
【问题描述】:

在我的 node.js 服务器上,我需要将一些 JSON 转换为 RSS 提要。做这个的最好方式是什么?完成转换后,它需要输出/覆盖一个 RSS 文件。

【问题讨论】:

    标签: javascript xml json node.js rss


    【解决方案1】:

    考虑使用https://github.com/dylang/node-rss

    示例:

    test.json:

    {
      "title": "foo",
      "description": "bar",
      "author": "hello"
    }
    

    test.js:

    var fs = require('fs')
      , RSS = require('rss');
    
    fs.readFile('test.json', function(err, data) {
      if (!err) {
        feed = new RSS(JSON.parse(data));
        fs.writeFile('feed.xml', feed.xml());
      }
    });
    

    运行node test.js 将生成feed.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
      <channel>
        <title> <![CDATA[foo]]></title>
        <description><![CDATA[bar]]></description>
        <link>http://github.com/dylan/node-rss</link>
        <generator>NodeJS RSS Module</generator>
        <lastBuildDate>Fri, 13 Jan 2012 03:44:22 GMT</lastBuildDate>
      </channel>
    </rss>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 2012-07-25
      • 2019-05-23
      • 1970-01-01
      • 2018-03-03
      相关资源
      最近更新 更多