1

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);
Paul Boon
  • 143
  • 1
  • 12
  • Can you post the code you've tried, and your folder structure? – Tennyson H Feb 09 '16 at 19:28
  • the js file and the xml are in the same folder and this is one of the thing i have tried "xhttp.open("GET", "delayReport.xml", true);" – Paul Boon Feb 09 '16 at 19:31
  • @PaulBoon edit the whole code sample into your question, please. One line isn't enough to know what might be going wrong. Also, just wondering: if you go to delayReport.xml in your browser, does it load the file? – dtanders Feb 09 '16 at 19:34
  • yes that loads in but only if i use file:///path/to/file to get there – Paul Boon Feb 09 '16 at 19:37
  • @PaulBoon OK good - I just wanted to make sure your server wasn't preventing access to it in general – dtanders Feb 09 '16 at 19:45
  • while i could use a server isn't this somehow possible using only a static html file + a javascript file – Paul Boon Feb 09 '16 at 19:46
  • @PaulBoon not over ajax – dtanders Feb 09 '16 at 19:54
  • Is your server serving the XML file or not? And are you trying to do this client side or through node.js? If it's client side, why are you showing node.js code at all? – zero298 Feb 09 '16 at 20:16

2 Answers2

1

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

Community
  • 1
  • 1
Andreas Müller
  • 210
  • 2
  • 10
1

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.

zero298
  • 25,467
  • 10
  • 75
  • 100