0

so I need to load the data from a xml file and then print it in a console...

For now im only taking the xml directly like this:

var xml = '<?xml version="1.0" encoding="UTF-8"?><Person><Name></Name></Person>'
var xmlDoc = $.parseXML( xml ); 
var $xml = $(xmlDoc);

But what I would like to do is instead pasting the xml file directly, to paste only the location to it, for example:

var xml = "C:User/xmls/example.xml";
  • Assuming you are running this from a server, you cannot access files locally on the computer. Security measures prevent this. – TimoStaudinger Jul 14 '16 at 20:52
  • You might be able to use FileReader https://developer.mozilla.org/en-US/docs/Web/API/FileReader – Philipp Braun Jul 14 '16 at 20:55
  • actually they are not locally kept, i have them on some location, but i assumed the logic would be similar. – Elena Matovska Jul 14 '16 at 20:55
  • I tried doing this var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", location, true); xmlhttp.send(); but when i go to get the response with xml.responseXML; its null. – Elena Matovska Jul 14 '16 at 20:57
  • if `responseXML` is null, than the XML is not valid, or you are not setting the right headers, or there is an error with the request. Do you see the responseText? – epascarello Jul 14 '16 at 21:02
  • the xml is ok because it works when i use it directly, for example if i use a mapping to xml that doesnt exist it returns an error, but with this only the response is null – Elena Matovska Jul 14 '16 at 21:17

1 Answers1

0

You could use an XMLHttpRequest() to get the file using its url.

var x = new XMLHttpRequest();
x.open("GET", "http://yoururl.com");
x.onreadystatechange = function () {
  if (x.readyState == 4 && x.status == 200)
  {
    var doc = x.responseXML; // Contains your requested document
  }
};
x.send(null);
Philipp Braun
  • 1,583
  • 2
  • 25
  • 41
  • As i said, the responseXML is null, maybe because I dont have actual url it looks something like this: data/message/example.xml this is what i want to get. – Elena Matovska Jul 14 '16 at 21:05
  • Accessing local files using Javascript is simply a really really bad idea. http://stackoverflow.com/questions/371875/local-file-access-with-javascript for further information. – Philipp Braun Jul 14 '16 at 21:07
  • Probably they are kept on some server, but i only have a mapping to use so i can get to them – Elena Matovska Jul 14 '16 at 21:12
  • If your files are not stored in the webserver directory you cannot really do this with pure Javascript. You could use Node.js on the server side though. But probably it would just be way easier to store the files in a proper "webserver" directory and run `XMLHttpRequest`. – Philipp Braun Jul 14 '16 at 21:19
  • I have them on a location like this bundleFilePath: "bundles/I/A/1/bundle.xml", and have to extract the xml from here – Elena Matovska Jul 14 '16 at 21:22
  • How do you run your javascript files? What's your ip. If you have a relative path you can also create an absolute path. Is this a location on the webserver? – Philipp Braun Jul 14 '16 at 21:24
  • Yes files are probably kept on a servev.(sorry i have limited knowledge) if I had an absolute path to a xml then how would I take its values? – Elena Matovska Jul 14 '16 at 21:30
  • @ElenaMatovska With an absolute url path you can exactly do it like in the example I provided. Please accept the answer if it has solved your question. – Philipp Braun Jul 16 '16 at 22:23