如何使用 node.js 从网上获取 xml 并将其解析为 javascript 对象? 我一直在搜索 npm 寄存器,但只找到了如何解析 xml 字符串,而不是如何获取它。
最佳答案
要获取在线资源,您可以使用http.get()
。数据可以加载到内存中,或者直接发送到 XML 解析器,因为有些支持解析流的特性。
var req = http.get(url, function(res) {
// save the data
var xml = '';
res.on('data', function(chunk) {
xml += chunk;
});
res.on('end', function() {
// parse xml
});
// or you can pipe the data to a parser
res.pipe(dest);
});
req.on('error', function(err) {
// debug error
});
关于javascript - 你如何在 node.js 中获取和解析 xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19195207/