0

I'm trying to read an xml file so I can learn how to, I have my test xml file in local and I'm trying to open and parse it.

All I find when I google for info is people asking how to solve their problems when parsing, but none of them explains how to open the file in first place.

I found this code:

if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","entrada.xml",false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;

But when testing on browser (Chrome) I get this error:

XMLHttpRequest cannot load file:///Users/.../cursos/www/entrada.xml. Cross origin requests are only supported for HTTP.
Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///Users/.../cursos/www/entrada.xml'. 

I don't want to use a device if can avoid. How can I test it on browser??

EDIT:

Found a way to make it work thanks to @Regent and @mehsen.

I needed to get the file in a different way if on browser or device as said here, and then, one I have the file, read it as said here and here.

Community
  • 1
  • 1
Sascuash
  • 3,661
  • 10
  • 46
  • 65
  • Did you look at http://stackoverflow.com/questions/8449716/cross-origin-requests-are-only-supported-for-http-but-its-not-cross-domain http://stackoverflow.com/questions/20041656/xmlhttprequest-cannot-load-file-cross-origin-requests-are-only-supported-for-ht http://stackoverflow.com/questions/22360700/error-message-xmlhttprequest-cannot-load-cross-origin-requests-are-only-suppor ? – Regent Jul 11 '14 at 08:33
  • If your xml file is on the local machine you donot need ajax request to get it . try some thing like filereader in javascript to read files. look at this [question1](http://stackoverflow.com/questions/5744064/html5-file-api-reading-in-an-xml-text-file-and-displaying-it-on-the-page) and [question2](http://stackoverflow.com/questions/20853219/how-to-read-xml-file-using-filereader-javascript) these may help ;) – mehsen Jul 11 '14 at 08:38
  • Thanks both, you helped me, I'll look your links and say something when done ;) – Sascuash Jul 11 '14 at 09:03
  • Ok, I found a way to make it work using both of your answers. You were really helpfull. Thanks! – Sascuash Jul 11 '14 at 09:05

0 Answers0