1

I am writing a local HTML file that needs to access an XML file that sits right next to the HTML. I can't figure out a simple way to load the contents of the XML file? This has to work on a local system without a server/localhost.

JQuery / Ajax is not working without a server/localhost, so I figured why not load the XML the same as JS or CSS files? Is this possible?

<head>
    <title>Local html file</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

    <!-- here we have a local css file -->
    <link rel="stylesheet" type="text/css" href="default.css" />

    <!-- here we have a local js file -->
    <script src="Main.js"></script>

    <!-- so why can't we have a local xml file? -->
    <link rel="xml" type="text/xml" href="myfile.xml" />
</head>
Kokodoko
  • 26,167
  • 33
  • 120
  • 197
  • What all have you tried so far? – Evan Sanderson Nov 21 '14 at 15:51
  • jQuery results in this error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load file. Using "" does not generate an error but it does not seem to load the file? – Kokodoko Nov 21 '14 at 15:57
  • You could try loading the xml in a hidden iframe. It doesn't appear that [Chrome would allow access](http://stackoverflow.com/questions/24603580/) to the DOM within the iframe, but maybe Firefox would? – rojo Nov 21 '14 at 16:02
  • This happened to me about a month ago. For some reason, the file won't open if it is a zip or jar extention. I had to extract my file and change its permissions to readable. I'm not sure if this is what is happening in your case, but your situation and the error you got mirrors what happened to me. – Evan Sanderson Nov 21 '14 at 16:03
  • I think the error is that xmlhttprequest cannot work without http, and local loading is not permitted. – Kokodoko Nov 21 '14 at 16:25

3 Answers3

2

You cannot do this easily with most browsers since they now tend to block local loading of files for "security reasons". Rubbish actually!

Anyway, there are a few choices:

  • Restrict yourself to using Firefox - which does allow local file loading
  • Write a browser plugin (yuck)
  • Use a a Java stub loader like TiddlyWiki does
  • Use a wrapper such as the excellent Node-Webkit
  • Give in and use Node.Js, Python or similar to run a mini server
  • For Windows only, switch to an HTML Application (HTA) which gives access to filing system libraries the same as Windows Scripting Host)
Julian Knight
  • 4,716
  • 2
  • 29
  • 42
  • Thanks :) Node-Webkit would be great, but sadly this HTML page will be pushed to a lot of clients who will run it locally on their own system. All I can deliver is a simple HTML page+xml file. I can't even rename to .HTA because I don't know if the client can open HTA files. – Kokodoko Nov 21 '14 at 16:20
  • You can package NW as a single executable if that helps. It might not though if you are talking about enterprise managed desktops. – Julian Knight Nov 21 '14 at 16:22
  • If that doesn't help, I think you are being unrealistic. Hmm, I wonder if you can do it with TiddlyWiki since that already has the library for loading/saving locally. – Julian Knight Nov 21 '14 at 16:30
  • All I can send to the client's system is a simple bare-bones html file. This has to load in their own custom display system that uses html. It will display my html but also others. I really can't do much except send pure html which has to run locally. (and it does, except for the xml!) – Kokodoko Nov 21 '14 at 16:31
  • Ah, well now we get to the nub of it. I think you need to rethink the approach. How about a pre-processor that combines and delivers a single file per client with the data built in. That WILL work. – Julian Knight Nov 21 '14 at 16:33
  • Good suggestion! But there's still a problem: the xml file WILL change over time, while the html file won't. The client can update the xml themselves. I cannot send them a new html file every time the data changes. (Well I could, but they don't pay me by the hour :) – Kokodoko Nov 21 '14 at 16:37
  • Where does the XML come from? It looks to me as though it is hobsons choice. Still it wouldn't be hard to get a small Node.JS service running to generate the combined files and make them available somewhere. – Julian Knight Nov 21 '14 at 16:39
  • The XML comes from a weather API. The "Broadsign" system periodically fetches new xml and stores it locally. Frustratingly, the 'parent' HTML file (the basis for the whole visual) cannot be refreshed this way and is completely static once it has been delivered. That is why I was searching for a way to load local xml. – Kokodoko Nov 21 '14 at 16:58
-1

No, this is not possible.

You can load it in with javascript though:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","myfile.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

xmlDoc now stores your xml.

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
-1

You can load the xml document in html by putting the below code in a function like this :

function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // code for IE5 and IE6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
julien carax
  • 323
  • 6
  • 22
  • This generates an error, I think it's because you cannot load local xml documents this way? ----- error code: XMLHttpRequest cannot load. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource. Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load – Kokodoko Nov 21 '14 at 16:11
  • This is, of course, a completely wrong answer. Not read the question. The clue is in the function name that contains HTTP which requires a server. – Julian Knight Nov 21 '14 at 16:26