0

I Please need to read xml file. If xml file changes i need to see the change in the index.html page, without reloading the entire page.

my code seems to work only on my local computer.

if i upload the code on the internet (on a Linux Hosting) the index.html reads xml file, but, if xml file changes, the index.html page never show any updates.

i noticed the two following :

after xml file changes (from another external script that updates the xml file), index.html does not update,

  • if i open "index.html" with mozilla firefox and press F5 to reload index.html page, it reloads that page, but never update data (still read old data from xml file).

  • if i open "data.xml" with mozilla firefox and i press F5, this will force the "index.html" to update.

of course none of the two above are considered a solution, because it has to work automatically without user interference, and of course without reloading the entire page.

Any help appreciated. Thanks.

this is my code

"index.html"

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body>
<div id="f1"></div><br>
<script>
function foo() {
    console.log("foo");
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "data.xml", false);
    xmlhttp.send();
    var xmlDoc = xmlhttp.responseXML;
    var f1 = xmlDoc.getElementsByTagName("f1")[0].childNodes[0].nodeValue;
    document.getElementById("f1").innerHTML = f1;
}
function onload() {
    console.log("onload");
    foo();
    setInterval(function(){
        foo(); 
    }, 2000);
}
onload();
</script>
</body>
</html>

this is data.xml

<?xml version="1.0" encoding="UTF-8"?>
 <outer>
    <inner>
        <f1>174</f1>
    </inner>
 </outer>
Mario
  • 1
  • It is obviously your call but if you have PHP (or other server language) [Server Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) would be, IMO, a much cleaner approach than long polling AJAX. It also means the actual reading of the `data.xml` file would be done server-side – Professor Abronsius May 14 '21 at 08:02
  • What is your console telling you? Take a look un how to controle cache: https://stackoverflow.com/a/2068407/3710053 – Siebe Jongebloed May 14 '21 at 08:07
  • One thing you might try would be to call the XML file with a timestamp appended to the querystring... ie: `'data.xml?t='+( new Date().getTime() )` etc – Professor Abronsius May 14 '21 at 09:09
  • Thanks to both of You. Professor Abronsius You save my day. Appending Time stamp did the trick. Thank You. – Mario May 14 '21 at 17:25

0 Answers0