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

I need a way to take some json and convert it to xml using node.js

本文关键字:node js xml 转换 一种 获取 方法 json      更新时间:2023-09-26

在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>