节点fs.readstream()输出<缓冲器3c 3f 78 6d 6c ...>代替可读数据 [英] Node fs.readstream() outputs <Buffer 3c 3f 78 6d 6c ...> instead of readable data

查看:123
本文介绍了节点fs.readstream()输出<缓冲器3c 3f 78 6d 6c ...>代替可读数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取Node Js中的一个大型XML文件(〜1.5gb).我正在尝试对其进行流传输并使用大量数据来执行某些操作,但是我发现很难理解文档.

I'm reading a large XML file (~1.5gb) in Node Js. I'm trying to stream it and do something with chunks of data, but I'm finding it difficult to understand the documentation.

我当前的简单代码是:

var fs = require('fs');

var stream = fs.createReadStream('xml/bigxmlfile.xml');

stream.on('data', function(chunk){ 
    console.log(chunk)
});

控制台提供了许多buffer十六进制(我认为)代码,如下所示:

The console gives a bunch of buffer hex (I think) codes like this:

<Buffer 65 61 6e 2d 63 75 74 20 67 72 69 64 20 6c 69 6e 65 73 20 74 68 65 20 73 70 72 65 61 64 20 63 6f 6c 6c 61 72 20 61 6e 64 20 6d 69 74 65 72 65 64 2c 20 74 ...>
<Buffer 65 79 77 6f 72 64 73 3e 3c 2f 6b 65 79 77 6f 72 64 73 3e 3c 75 70 63 3e 34 32 39 35 36 30 31 33 38 33 38 39 3c 2f 75 70 63 3e 3c 6d 31 3e 36 38 38 39 31 ...>
<Buffer 6f 75 6e 74 3e 3c 63 75 72 72 65 6e 63 79 3e 55 53 44 3c 2f 63 75 72 72 65 6e 63 79 3e 3c 2f 63 6f 73 74 3e 3c 69 6e 66 6f 72 6d 61 74 69 6f 6e 3e 3c 2f ...>
<Buffer 65 20 62 72 69 65 66 73 20 74 68 61 74 20 73 69 74 20 63 6f 6d 66 6f 72 74 61 62 6c 79 20 61 74 20 74 68 65 20 68 69 70 73 2e 20 43 6f 6c 6f 72 28 73 29 ...>
<Buffer 3c 64 65 73 63 72 69 70 74 69 6f 6e 3e 3c 73 68 6f 72 74 3e 43 7a 65 63 68 20 63 72 79 73 74 61 6c 73 20 73 70 72 69 6e 6b 6c 65 20 61 20 73 6c 69 6e 67 ...>

我也尝试过:

var fs = require('fs');
var parseString = require('xml2js').parseString;

var stream = fs.createReadStream('xml/lsnordstrom.xml');

stream.on('data', function(chunk){ 
    //do something on file data
    parseString(chunk, function (err, result) {
        console.log(result);
    });
});

(这样我就可以将XML流解析为JSON),但是在控制台中却得到undefined结果.

(so I can read parse the XML stream as JSON) but I get undefined results in the console.

我实际上如何将这些数据转换为有用的东西?

How do I actually convert this data into something useful?

推荐答案

您可以像这样设置流编码:

You can set the stream encoding like so:

var stream = fs.createReadStream('xml/lsnordstrom.xml');
stream.setEncoding('utf8');

或将缓冲区转换为字符串:

Or convert the buffers to strings:

stream.on('data', function(chunk) { 
  chunk.toString('utf8');
});

此外,要像您尝试的那样解析XML,您将需要一个流解析器.

Also, to parse XML like you're trying to do, you'll need a stream parser.

这篇关于节点fs.readstream()输出&lt;缓冲器3c 3f 78 6d 6c ...&gt;代替可读数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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