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. There are multiple xml files to be read so the same process is repeated.
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. But when it is traced with console.log() just before the return statement, it becomes an empty array.
How would you fix this? Please feel free to comment. Any help or advice is appreciated.