繁体   English   中英

您如何获取和解析 node.js 中的 xml?

[英]How do you fetch and parse xml in node.js?

如何使用 node.js 从在线获取 xml 并将其解析为 javascript ZA8CFDE6331BD49EB2AC96F866? 我一直在搜索 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
});

这也应该有效。

const request = require("request-promise");

const web_url = "https://www.gillmanacura.com/sitemap.xml";

(async() => {
    let emptyData = [];   
        let resp = await request({
            uri: web_url,
            headers:{},
            timeout:10000,
            json: true,
            gzip: true
        });        
        console.log(resp)       
})();

使用节点获取<\/h1>

这是一个使用node-fetch<\/a>检索 xml 的示例。

安装较低版本的 node-fetch 以使用 require 或使用常规 ESM 导入。

npm install node-fetch@^2.6.6

Obrigado pela ajuda,朋友。 Vc é realmente um amigo,朋友。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM