我在解析Node / Express服务器中附加XML文件的大文件时遇到麻烦。我正在下载一个zip文件,然后将其管道传输到一个流中,在该流中,我读取每行分析文档的内容。
原始文档是一系列彼此附加的XML文档。
当我console.log XML字符串时,它显示了整个文档。
出于某种原因,当我将XML字符串传递给JSON解析器时,结果是不确定的。我已经确认解析器已经可以工作了。我还确认了解析器也可以在其中一个XML文档上工作,因此格式并不错误。我还将复制输出的xml字符串复制到XML格式化程序中,并且它可以正确格式化。
我不确定我在做什么错,是因为xmlDoc字符串太大吗?
const path = Path.resolve(__dirname, "myFolder", "myZip.zip");
const unzip = require("child_process").spawn("unzip", ["-c", path]);
const xml2js = require("xml2js");
const es = require("event-stream");
var index = 0
var docNo = 0;
var xmlDoc = "";
var s = unzip.stdout.pipe(es.split()).pipe(
es
.mapSync(function(line) {
// pause the readstream
s.pause();
//only here to parse through the first document
if(index < 1){
// process line below and call s.resume() when ready
// Get the document ready
if (line.startsWith("<?xml")) {
docNo += 1;
console.log(docNo);
xmlDoc += line;
// If the line is the end of the document
} else if (line.startsWith("</end-doc")) {
xmlDoc += line;
var parser = new xml2js.Parser();
parser.parseString(xmlDoc, function(err, json) {
// This prints out the xml string
console.log(xmlDoc);
// But this is undefined!!!!!
console.log(json);
addJsonToDb(json);
xmlDoc = ""
});
// Else add the line to the end of the document
} else {
xmlDoc += line;
}
s.resume();
}
})
.on("error", function(err) {
console.log("Error while reading file.", err);
})
.on("end", function() {
console.log("Read entire file.");
})
);
我在解析Node / Express服务器中附加XML文件的大文件时遇到麻烦。我正在下载一个zip文件,然后将其通过管道传输到一个流中,在该流中,我阅读每行解析文档的信息。 ...
似乎有关解压缩过程的信息已预先添加到xmlDoc变量中。肮脏的把戏:可以通过测试line变量包含“ inflating”来快速解决,如下所示: