notme
notme

Reputation: 461

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?

Upvotes: 1

Views: 640

Answers (3)

Jeff Sloyer
Jeff Sloyer

Reputation: 4964

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

Upvotes: 1

formixian
formixian

Reputation: 1559

Try 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.

Upvotes: 0

jpapejr
jpapejr

Reputation: 211

Personally, I'd :

  1. Read in the XML file
  2. Convert the XML model into a JSON object
  3. Then use the nano npm library to insert the data into Cloudant

Upvotes: 2

Related Questions