SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax: Retrieving data from multiple XML files

    Hi everyone,

    I have a function that loops through a list of objects, and for each one makes an Ajax request to load an XML file, and fill a couple of the objects variables with elements from that XML file. The problem is that when execute, the function only fills one or two of the objects. I am assuming this has something to do with the function being called multiple times in a row and not having a chance to finish the previous call. If this is the case, is there a way to force the code to pause while it waits for the previous one to execute fully?

    Code:
    var oObjects = [
      {
        name:'Title1',
        image:'Title1.png',
        xml: 'Title1.xml',
        img: '',
        tbl: '',
        style: ''
      },
      {
        name:'Title2',
        image:'Title2.png', 
        xml: 'Title2.xml',
        img: '',
        tbl: '',
        style: ''
      }
    ];
    
    var http_request = false;
    
    function loadXML(iObj)
    {
      http_request = false;
    
      if (window.XMLHttpRequest)
      {
        http_request = new XMLHttpRequest();
      }
      else if (window.ActiveXObject)
      {
        try
        {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
          try
          {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch (e) {}
        }
      }
    
      if (!http_request)
      {
        alert('Cannot create an XMLHTTP instance');
        return false;
      }
    
      http_request.onreadystatechange = function() { fillXMLdata(iObj) };
      http_request.open('GET', oObjects[iObj].xml, true);
      http_request.send(null);
    
      return true;
    }
    
    function fillXMLdata(n)
    {
      if (http_request.readyState == 4)
      {
        if(http_request.status == 200 || http_request.status == 0) // http_request.status == 0 for testing locally - remove before deployment.
        {
          var xmlObj = http_request.responseXML;
    
          var t = xmlObj.getElementsByTagName("data");
          oObjects[n].tbl = t[0].childNodes[0].nodeValue;
          var s = xmlObj.getElementsByTagName("styles");
          oObjects[n].style = s[0].childNodes[0].nodeValue;
        }
      }
    }
    
    function run() // Entry point
    {
      for (var i = 0; i < oObjects.length; i++)
      {
        loadXML(i);
      }
    
      for (var x = 0; x < oObjects.length; x++)
      {
        alert('oObjects[' + x + '] Loaded:\n ' + oObjects[x].style);
      }
    }
    In my real code there could be 100+ objects. When it loops through and displays the styles, it is only showing up for the last one when run locally. If run from the server, it might show one more in the middle somewhere.

    I would greatly appreciate any advice.

    Kind regards,
    Jordan

  2. #2
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, so I have a solution that is a really crappy way of handling it. I have created a global variable named porcessing, and when a call is made to load an XML file it sets processing = true. Once it has loaded the file and extracted the data it needs, it changes it back to processing = false.

    Then, in my main procedure I have the following:
    Code:
    do
    {
       // Nothing...
    }
    while (processing == true);
    
    loadXML(i);
    So this will halt the system in myNothing loop until the XML file is loaded before moving on to the next file. The problem with this, however, is that it halts the entire browser. I want this to happen in the background, so I am assuming that I can't put the system in to this loop.

    Anyone have any other ways of handling this please? The only other thing I can think of is to have the function that extracts the data call the load function again with the next number as an argument, so that it will be called only once the data is liaded, but I haven't tried this yet. Might look in to it now.

    Regards,
    Jordan


Bookmarks

Posting Permissions

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