Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Load an XML File - Cross-browser

    So I was reading up on how to load and parse an XML file into an html page and I found through w3 a great tutorial with sample code:

    http://www.w3schools.com/xml/xml_dom.asp

    It appears to work fine on their website but when I try it from my desktop it doesn't work. So what I have is an html file with contents:
    Code:
    <html>
    <body>
    <h1>W3Schools Internal Note</h1>
    <div>
    <b>To:</b> <span id="to"></span><br />
    <b>From:</b> <span id="from"></span><br />
    <b>Message:</b> <span id="message"></span>
    </div>
    
    <script type="text/javascript">
    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","note.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    
    document.getElementById("to").innerHTML=
    xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
    document.getElementById("from").innerHTML=
    xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
    document.getElementById("message").innerHTML=
    xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
    </script>
    
    </body>
    </html>
    And then the xml file named "note.xml":

    Code:
    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    I have no idea what I am missing or not seeing that is causing this not to work but if anyone knows I would appreciate the input. For the record I have tried this in chrome and ie.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    There are two reasons why
    the code you posted will not
    work with IE on the file system.
    It does work with Firefox,
    the following code works with
    firefox and IE, as long as you
    have note.xml on your desktop
    with the htm file .

    Code:
    <html>
    <body>
    <h1>W3Schools Internal Note</h1>
    <div>
    <b>To:</b> <span id="to"></span><br />
    <b>From:</b> <span id="from"></span><br />
    <b>Message:</b> <span id="message"></span>
    </div>
    
    <script type="text/javascript">
    
    var xmlhttp=false;
    var ie = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
     ie=true;
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
    		xmlhttp = new XMLHttpRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    if (!xmlhttp && window.createRequest) {
    	try {
    		xmlhttp = window.createRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
      
    xmlhttp.open("GET","note.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    if(ie){
    xmlDoc = new ActiveXObject ("microsoft.xmldom");
    xmlDoc.loadXML(xmlhttp.responseText); 
    }
    
    
    document.getElementById("to").innerHTML=
    xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
    document.getElementById("from").innerHTML=
    xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
    document.getElementById("message").innerHTML=
    xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
    </script>
    
    </body>
    </html>And then the xml file named "note.xml":
    The first reason is IE XMLHttpRequest won't
    normally read the file system but ActiveXObject
    will read file system.
    Second IE only gives responseXML if the
    file has XML headers , you get those
    headers from server not file system.
    Last edited by DaveyErwin; 10-12-2011 at 10:05 PM.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Works for IE but not for Chrome

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by shaggykyle View Post
    Works for IE but not for Chrome
    This is because of a security
    setting in Chrome, go to
    your run box and type in ...
    chrome --allow-file-access-from-files
    now when chrome opens
    it will read from file system.
    Last edited by DaveyErwin; 10-12-2011 at 10:52 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •