xml2js从URL读取 [英] xml2js read from url

查看:127
本文介绍了xml2js从URL读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有NodeJS并安装了模块xml2js.在本教程中,我们有一个示例,该示例从目录中获取一个xml文件,并使用JSON.stringify()将其转换为示例.现在可以代替调用本地xml文件(foo.xml)来调用XML服务的URL,例如:

So I have NodeJS and installed the module xml2js. In the tutorial we have an example taking an xml file from directory and convert it with JSON.stringify() as in the example. Now is there a possibility instead of calling the local xml file (foo.xml), to call a url of XML service for ex: www.wunderground.com/city.ect/$data=xml

var parser = new xml2js.Parser(); 
parser.addListener('end', function(result) {
    var res = JSON.stringify(result);   
        console.log('converted'); 
}); 
fs.readFile(__dirname + '/foo.xml', function(err, data) {
    parser.parseString(data); 
});

推荐答案

您需要创建一个http请求,而不是读取文件.我想是这样的:

You need to create an http request instead of reading a file. Something like this, I think:

http.get("http://www.google.com/index.html", function(res) {
  res.on('data', function (chunk) {
    parser.parseString(chunk); 
  });
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

http://nodejs.org/api/http.html#http_http_request_options_callback

这篇关于xml2js从URL读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆