4

I have this code, where I run parseString()to extract some information from an xml file

function parseTime(){

var parser = new xml2js.Parser();
var data = fs.readFileSync('C:\\Temp\\tasks\\acis\\110-1100.sat\\110-1100.sat.response.xml', {encoding:'utf8'});

parser.parseString(data, function (err, result) {

var timeString = result.Message.Response[0].Events[0].MessageReportEvent[8].$.Message;
var fileTime = timeString.substr(13,20);
var filetimeVal = parseFloat(fileTime);

console.log(filetimeVal);

return filetimeVal;

});

};

What changes should I do to run parseString synchronously or is there a way to extract the xml data via a deifferent synchronous method

2
  • 2
    Did you look at the API documentation of this library? Do they provide a synchronous method? If yes, use that, if not, you are out of luck. Change your code to work asynchronously. Commented Nov 20, 2015 at 1:55
  • 1
    the documentation implies that the callback is called synchronously, there's an option to make it async - github.com/Leonidas-from-XIV/node-xml2js#options Commented Nov 20, 2015 at 2:12

1 Answer 1

1

The callback of the parseString already runs synchronously. Your program would reach the parsed results before it reaches any other line of code.

There is async switch which is false by default in the options.

However the developers warn that this default might change in the future.

async (default false): Should the callbacks be async? This might be an incompatible change if your code depends on sync execution of callbacks. Future versions of xml2js might change this default, so the recommendation is to not depend on sync execution anyway. Added in 0.2.6.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.