通过node.js将XML文件加载到cloudant
我在Hackathon,我们必须使用IBM Bluemix技术。 我们都是NodeJ和IBM Bluemix的新手。
我们需要上传这个 XML(也有TTL,RDF和N3格式),以创build一个数据库并上传所有内容。
你有什么build议如何做到这一点?
下面的例子和代码假设你正在使用Express 3. Express 4与正文parsing有点不同…
- 发布文件来expression。
- 阅读上传的文件
- 将XML转换为JSON。
-
将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("/"); } ); });
就个人而言,我会:
- 阅读XML文件
- 将XML模型转换为JSON对象
- 然后使用nano npm库将数据插入到Cloudant中
尝试damn-simple-xml转换为JavaScript对象。 这很容易使用,这里有一个简单的例子。