Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loading asynchronous XML in javascript

Posted on 2007-11-19
4
Medium Priority
?
614 Views
Last Modified: 2013-11-19
Hello,

Im using javascript to load a simple xml document, using the following code:

      //Name: loadXML
      //Purpose: To load xml at any location
      var xmlDoc;
      function loadXML(strPath) {
            
            //code for IE
            if(window.ActiveXObject) {
                  
                  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                  xmlDoc.async=false;
                  //xmlDoc.onreadystatechange = getXMLState;
                  xmlDoc.load(strPath);
                  return xmlDoc;
                  
                  } else if(document.implementation && document.implementation.createDocument) { // mozilla
                        
                        xmlDoc = document.implementation.createDocument("","",null);
                        xmlDoc.load(strPath);
                        return xmlDoc; // returning data before entire document is loaded
      
                        }
            
            }


And i am calling the code like so:

      //load the xml data
      var objXmlTmp = loadXML('/data/formData2.xml');
      
      //alert('Number of objXmlTmp nodes = ' + objXmlTmp.childNodes.length);
      
      //get the nodes
      var objXml = objXmlTmp.getElementsByTagName('list');
      
      //alert('Length of objXml nodes in top function = ' + objXml.length);

My problem is that if you uncomment the last alert statement, it will return "Length of objXml nodes in top function = 0", however if you additionally uncomment the first alert statement, the last alert statement will return "Length of objXml nodes in top function = 3". I am therefore convinced that this is because Firefox is calling the XML asynchronously, and by executing the alert statements the code is having enough time to finish loading the xml.

Can anyone help modify my code so that the firefox version also loads asychronously?

Thanks in advance
Al
0
Comment
Question by:partnershipdev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 20313135
try this

http://www.webmasterworld.com/forum91/3517.htm
var oLoadedXML;
function loadXML(sImportXML) {
  if( window.ActiveXObject && /Win/.test(navigator.userAgent) ) {
    oLoadedXML = new ActiveXObject("Msxml.DOMDocument");
    oLoadedXML.async = false;
    oLoadedXML.onreadystatechange = function () {
      if (oLoadedXML.readyState == 4) parseXML();
    }
    oLoadedXML.load(sImportXML);
  }
  else if( document.implementation && document.implementation.createdocument ) {
    oLoadedXML = document.implementation.createdocument("","",null);
    oLoadedXML.async=false;
    var loaded = oLoadedXML.load(sImportXML);
    if (loaded) {
      parseXML();
    }
  }
  else {
    alert("Your browser can\'t handle this script");
    return;
  }
} 

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20313162
where you have
function parseXML() {
  var objXml = oLoadedXML.getElementsByTagName('list');
  alert('Length of objXml nodes in top function = ' + objXml.length);
}

Open in new window

0
 

Author Comment

by:partnershipdev
ID: 20318655
Hi mplungian,

Thanks for your input. I didnt use the code exactly, but was able to use parts of it in my existing code which now works perfectly.

Probably the quickest response ive ever had on EE.

Cheers
Al
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20319532
Ah..  I have been faster than that... If I knew the answer off the top of my head it could have been under 2 minutes ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question