I'm trying to read some information from a local XML document however i cant figure out how to do it.
I'm able to include js(and read data from them) css and all those kind of files but how do i read from an XML file or any other file.
The XML file is in the same directory as the js file so why shouldn't i be able to read it like any other js file?
I know it's possible to read files using nodejs but i'm trying to read the xml data and display it in a html page so it has to be client side.
// kan tijdelijk delayreport in js file zetten
var fs = require('browserify-fs'),
xml2js = require('xml2js');
var parser = new xml2js.Parser();
fs.readFile('delayReport.xml', function(err, data) {
parser.parseString(data, function (err, result) {
//console.dir(JSON.stringify(result,null,2));
console.log('Done');
console.log(result);
});
});
xhttp.open("GET", "delayReport.xml", true);
Generally it's not possible, because the XHMLRequest needs a server for asynchron communication. Only Browser that I know that loads local files offline is firefox. but maybe there are some workarounds, like discussed here xmlhttprequest for local files
Don't rely on the file://
protocol. Set up a local server and host your XML file statically through that. Then you will be able to request your XML file with an XHR. Fighting file://
is going to give you headaches.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.