通过 node.js 将 XML 文件加载到 cloudant

Load a XML file to cloudant by node.js

我正在参加黑客马拉松,我们必须使用 IBM Bluemix 技术。 我们都是 NodeJs 和 IBM Bluemix 的新手。

我们需要上传thisXML(也有TTL、RDF、N3格式)创建数据库的方式上传所有内容

你对如何做到这一点有什么建议吗?

就个人而言,我会:

  1. 读入 XML 文件
  2. 将 XML 模型转换为 JSON 对象
  3. 然后使用nano npm library将数据插入Cloudant

尝试damn-simple-xml to convert to a JavaScript object. It's easy to use and there is a good example of a simple case here

以下示例和代码假定您使用的是 Express 3。Express 4 的正文解析略有不同...

  1. Post要表达的文件。
  2. 读入上传的文件
  3. 将 XML 转换为 JSON。
  4. 将 JSON 存储在您的数据库中。

    app.post("/upload", function (request, response) {
        async.waterfall(
            [
                function (next) {
                    //where leads is the name of the field from your html page
                    fs.readFile(request.files.leads.path, next);
                },
                function (xml, next) {
                    var json = parser.toJson(xml);
                    db.insert(json, next)
                },
            ], function (error) {
                if (error) {
                    console.log(error);
                    response.send(error);
                    return;
                }
                response.redirect("/");
            }
        );
    });