简体   繁体   English

Express.js读取xml文件数据并将数据作为数组传递

[英]Express.js read xml file data and pass the data as an array

I'm trying to build a Node.js web app with Express.js, that reads values from an external xml file, and store all the data values in a single array. 我正在尝试使用Express.js构建Node.js Web应用程序,该应用程序从外部xml文件读取值,并将所有数据值存储在单个数组中。 There are multiple xml files to be read so the same process is repeated. 有多个XML文件要读取,因此重复相同的过程。

function loadSoftwareRequestXML(filename){
var xmlparser = new xml2js.Parser();
var software_request = new Array();
var filepath = "/project_requests/" + filename;
fs.readFile(filepath, "utf-8", function(error, values){
    xmlparser.parseString(values, function(error, xmlfile){
        var xmldata = xmlfile;
        date_requested = xmldata.ProjectRequest.DateRequested;
        client_org = xmldata.ProjectRequest.ClientOrganization;
        proposed_budget = xmldata.ProjectRequest.ProposedBudget;
        contact_name = xmldata.ProjectRequest.ContactName;
        delivery_date = xmldata.ProjectRequest.DeliveryDate;
        requirements = xmldata.ProjectRequest.UserRequirements;
        //software_request = [date_requested, client_org, proposed_budget, contact_name, delivery_date, requirements];
        software_request.push(date_requested);
        software_request.push(client_org);
    });
});
console.log(software_request);
return software_request;
}

The problem I'm having is that for 'software_request', the array variable that stores the retrieved xml data, it works when it is inside the xmlparser function. 我遇到的问题是,对于“ software_request”,存储检索到的xml数据的数组变量,当它位于xmlparser函数内部时,它可以工作。 But when it is traced with console.log() just before the return statement, it becomes an empty array. 但是,当在return语句之前使用console.log()对其进行跟踪时,它将成为一个空数组。

How would you fix this? 您将如何解决? Please feel free to comment. 请随意发表评论。 Any help or advice is appreciated. 任何帮助或建议,不胜感激。

In node.js, I/O is asynchronous so functions like readFile and parseString do not complete in the same event loop that they are called. 在node.js中,I / O是异步的,因此readFileparseString类的函数不会在调用它们的同一事件循环中完成。 This means that code that is written after them will be called first. 这意味着将首先调用在它们之后编写的代码。

When an asynchronous event completes, there are many ways to signify its completion and react to it. 当异步事件完成时,有很多方法可以表示其完成并对其做出反应。 Callbacks are very common in node.js. 回调在node.js中非常普遍。 The function (error value) that you pass as arguments to these functions are called when the asynchronous event is done. 当异步事件完成时,将调用您作为参数传递给这些函数的function (error value)

fs.readFile(file, cb);
return software_request; // this gets called before `cb`

To keep this pattern, you can pass a callback to loadSoftwareRequestXML 为了保持这种模式,您可以将回调传递给loadSoftwareRequestXML

function loadSoftwareRequestXML(filename, cb){
    const xmlparser = new xml2js.Parser();
    const software_request = [];
    const filepath = "/project_requests/" + filename;
    fs.readFile(filepath, "utf-8", function(error, values){
        xmlparser.parseString(values, function(error, xmlfile){
            /* snip */
            console.log(software_request);
            cb(error, software_request);
        });
    });
}

Essentially, you can't return from callbacks. 本质上,您不能从回调return

You should also handle the errors if they are there. 如果错误在那里,您也应该处理。

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

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