www.webdeveloper.com
Results 1 to 4 of 4

Thread: xml file access trouble

  1. #1
    Join Date
    Apr 2012
    Posts
    4

    xml file access trouble

    Hi everyone, I am trying to access an online XML file, for example: http://www.rgraph.net/sample.xml using HTML.

    <html>
    <body>

    <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");
    }

    var url = "http://www.rgraph.net/sample.xml";
    xmlhttp.open("GET",url,false); //If url is replaced by the downloaded file named "sample.xml" this works and give the output of 1.
    // but works only on Mozilla
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;

    var x=xmlDoc.getElementsByTagName("stats");
    alert(x.length);

    </script>

    </body>
    </html>

    Any comment or advice would be much appreciated!

    PS: Why does this only work on Mozilla and NOT on Safari or Chrome?

    Many thanks!

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    The AJAX is a simple technique, but not so simple

    Before using the content of a file accessed via AJAX request, you should check the properties of the request object (readyState and status). And you should use the event onreadystatechange for that.

    And the DOM reference of the document of an XML file is a distinct object: documentElement

    A basic example:
    Code:
    <script type="text/javascript">
    var xmlhttp;
    
    function AJAX(url){
    xmlhttp=null;
    // Modern browsers
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      xmlhttp.url=url;
      xmlhttp.onreadystatechange=xmlhttpResponse;
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
      }
    // old IE
    else if (window.ActiveXObject)
      {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
        if (xmlhttp)
        {
    	xmlhttp.url=url;
        xmlhttp.onreadystatechange=xmlhttpResponse;
        xmlhttp.open("GET",url,true);
        xmlhttp.send();
        }
      }
    }
    
    function xmlhttpResponse()
    {
    // if loaded
    if (xmlhttp.readyState==4)
      {
      // if status is "OK"
      if (xmlhttp.status==200)
        {
    
    xmlDoc=xmlhttp.responseXML.documentElement;
    var x=xmlDoc.getElementsByTagName("stats");
    alert(x.length);
    
        }
      else
        {
        alert("The connection failed or the file does not exist!")
        }
      }
    }
    onload=function(){
    AJAX('sample.xml');
    }
    </script>
    Last edited by Kor; 04-20-2012 at 06:44 AM.

  3. #3
    Join Date
    Apr 2012
    Posts
    4
    Thank you Kor. However i am still unable to read the XML file from the other server. Could you help me with that?

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Xperia124 View Post
    Thank you Kor. However i am still unable to read the XML file from the other server. Could you help me with that?
    Simple AJAX is not able to perform requests cross-domains. You need an intermediary, a proxy server-side code to do that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles