简体   繁体   English

如何在node.js中使用xml文件?

[英]How do I make use of an xml file in node.js?

In my server.js file, I make an HTTP GET request that is suppose to return xml. 在我的server.js文件中,我提出了一个HTTP GET请求,该请求应该返回xml。 When I log the GET request's response to the console, it is gibberish containing lots of question marks and black diamonds as you can see in the photo below: 当我将GET请求的响应记录到控制台时,它看上去很乱,其中包含很多问号和黑色菱形,如下图所示: 在此处输入图片说明

When I take the same url that I'm using in my GET request and I open it in the browser it automatically downloads a gzip file, which after it's unzipped contains a legible xml file with the data (inside my text editor). 当我在GET请求中使用与我使用的相同的URL并在浏览器中打开它时,它会自动下载一个gzip文件,该文件解压缩后会包含带有数据的清晰xml文件(在我的文本编辑器中)。

How do I get the xml in its correct form inside my server.js file? 如何在server.js文件中获取正确格式的xml? I need to make use of it in my program, not inside a text editor (obviously). 我需要在程序中使用它,而不是在文本编辑器中使用(显然)。

Here is my GET request: 这是我的GET请求:

axios.get('http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l')
  .then(function(response) {
    console.log(response.data);
  });

I've tried to extract the gzip file using the targz library as shown below: 我尝试使用targz库提取gzip文件,如下所示:

axios.get('http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l')
  .then(function(response) {
    targz().extract(response.data, '/data', function(err){
      if (err) {
        console.log('Something is wrong ', err.stack);
      }
      console.log('Job done!');
    });
  });

I get an error in the console saying : "Path must be a string without null bytes". 我在控制台中收到一条错误消息:“路径必须是不包含空字节的字符串”。 Should I be using the extract method from targz or am I just using it incorrectly? 我应该使用targz中的extract方法还是使用不正确? I'm trying to "extract" or unzip the response.data. 我正在尝试“提取”或解压缩response.data。

Based on this: Simplest way to download and unzip files in Node.js cross-platform? 基于此: 在Node.js跨平台中下载和解压缩文件的最简单方法?

var feedURL = 'http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l';

var request = require('request'),
    zlib = require('zlib'),
    fs = require('fs'),
    out = fs.createWriteStream('./feed.xml');

request(feedURL).pipe(zlib.createGunzip()).pipe(out);

From the updated code, it appears you that the first parameter (response.data) needs to be set to a path on the filesystem of a gzip file, hence the null byte error. 在更新的代码中,您似乎需要将第一个参数(response.data)设置为gzip文件的文件系统上的路径,因此出现了空字节错误。 I would consider writing to the file system, then extract, or another module that would let you extract from a url. 我会考虑写入文件系统,然后解压缩,或者使用另一个模块从URL中解压缩。

When you do get the XML out of the extracted gzip file (which you are on the right path, no pun intended), you can use a node module such as xml2js , which will parse the xml into a Javascript object, and makes it quite easy to work with. 当确实从提取的gzip文件中获取XML(您在正确的路径上,没有双关语)时,可以使用诸如xml2js之类的节点模块,它将xml解析为Javascript对象,并使其相当易于使用。

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

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