javascript - 我需要一种方法来获取一些 json 并使用 node.js 将其转换为 xml

标签 javascript xml json node.js rss

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

最佳答案

考虑使用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>

关于javascript - 我需要一种方法来获取一些 json 并使用 node.js 将其转换为 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8845560/

相关文章:

java - 使用 Retrofit 2 处理多个 JSON 对象的正确方法是什么?

javascript - 模板中的数字总和

javascript - 谷歌图表如何通过点击图例隐藏线条

javascript - 奇怪的折线图

带类的 JSON?

javascript - 使用 .get 获取 json 数据 jquery

javascript - Angular4选择的选项在 "select"标签中选择后消失

java - 如何修复 "error: <item> inner element must either be a resource reference or empty."?

xml - RelaxNG 与 XML 架构

android - 我应该启动一个线程来解析一些 xml 吗?