Search code examples
xmlnode.jsparsingcloudantibm-cloud

Load a XML file to cloudant by node.js


I'm in a Hackathon and we have to use IBM Bluemix technologies. We are all new to NodeJs and IBM Bluemix.

We need to upload this XML (there are also TTL, RDF and N3 formats) in the way to create a db and upload everything into it.

Do you have any suggestion on how to do this?


Solution

  • The following example and code assume you are using Express 3. Express 4 is a little different with body parsing...

    1. Post the file to express.
    2. Read in the uploaded file
    3. Convert the XML to JSON.
    4. Store the JSON in your DB.

      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("/");
              }
          );
      });