在我的 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/