How To Retrieve XML Data Asynchronously with ondataavailable Event (285874)



The information in this article applies to:

  • Microsoft XML 2.6
  • Microsoft XML 3.0
  • Microsoft XML 4.0

This article was previously published under Q285874

SUMMARY

When you handle asynchronous data, it is useful to start processing in parallel with the download as soon as data becomes available. The ondataavailable event fires each time a new chunk of data arrives, so that you can do the transfer immediately. However, the onreadystate event fires when the document load is complete. In order to get the last chunk of data, you must process it when the readyState property equals 4 in the onreadystate event handler.

MORE INFORMATION

If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:

305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds

To explicitly use MSXML 4.0 in the code below, create the objects using the following:
  1. XSLDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument.4.0");
  2. XMLDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
  3. XSLTemplate = new ActiveXObject("MSXML2.XSLTemplate.4.0");
  1. Paste the following XML code into a new document and save the document as books.xml:
    <bookstore specialty="novel"
    xmlns:dt="urn:uuid:C2F41010-65B3-11d1-A29F-00AA00C14882/" 
    xmlns:my="urn:http//www.placeholder-name-here.com/schema/">
    	<book subject="Philosophy of Science">
    		<title>Nanotechnology:  50 Years To Eternity</title>
    		<author>
    			<first-name>Richard</first-name>
    			<last-name>Carey</last-name>
    		</author>
    		<award>Nobel Prize for Scientific Literature</award>
    		<price>12</price>
    	</book>
    	<book subject="Biography">
    		<title>Reid Wilkes:  The Man, The Myth</title>
    		<author>
    			<first-name>William</first-name>
    			<last-name>Shatner</last-name>
    		</author>
    		<price>55</price>
    	</book>
    </bookstore>
    					
  2. Paste the following XSL code into a new document, and save it as books.xsl in the same path where the preceding XML document is saved:
    <xsl:stylesheet version ='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>  
    	<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
    	<xsl:template match="/">
    		<xsl:for-each select="bookstore/book"> 
    			<b><xsl:value-of select="title"/></b><br/>
    			By: <xsl:value-of select="author"/><br/>
    			Cost: $<xsl:value-of select="price"/><br/>
    			<br/>
    		</xsl:for-each>
    	</xsl:template>  
    </xsl:stylesheet> 
    					
  3. Paste the following code into a new HTML document. Edit the document to refer to the path to the preceding XML and XSL documents that are referenced in the call to AsyncLoadXML():
    <html>
    <body onload="AsyncLoadXML('http://MyServer/books.xml', 'http://MyServer/books.xsl');">
    	<b>Output:</b>
    	<hr width="100%">
    	<div id="OutputDiv"/>
    </body>
    </html>
    
    <script language="JavaScript">
            var ChunkCount = 0; // Counts the chunks as they come through...
            var XMLDoc;         // XMLDOM document for downloading data
            var XSLDoc;         // XSL document to use for transformation
            var XSLTemplate;    // XSL Template object used to process the asynchronous transformation
            var XSLProcessor;   // XSL transformation engine
            var XSLOutput;      // HTML window to write results to. This function
                                // will kick off the data download and wire up the async callbacks
                                // that MSXML makes during the download. XSLTemplate is the compiled
                                // stylesheet.
    
    	function AsyncLoadXML(XMLPath, XSLPath)
    	{
    		// Initialize the XSL document...
    		XSLDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
    		XSLDoc.async = false;
    		XSLDoc.load(XSLPath);
    		
    		// Initialize the XML document...
    		XMLDoc = new ActiveXObject("MSXML2.DOMDocument");
    		XMLDoc.ondataavailable = TransformChunk;
    		XMLDoc.onreadystatechange = ReadyStateChange;
    		XMLDoc.async = true; 
    		
    		// Initialize the XSLTemplate document...
    		XSLTemplate = new ActiveXObject("MSXML2.XSLTemplate");
    		XSLTemplate.stylesheet = XSLDoc;
    		XSLProcessor = XSLTemplate.createProcessor();
    		XSLProcessor.input = XMLDoc; 
    		
    		// Finally, load the XML document asynchronously...
    		XMLDoc.load(XMLPath);
    		
    		// And display the output...
    		//OutputDiv.innerHTML = XSLOutput;
    	}
    	
    	// The following function is the async callback. Now you can transform the next chunk
    	// (as much as is possible, based on how much data was downloaded). 
    	// You can then get the chunk of transformed output and write it to 
    	// an HTML frame that incrementally displays the results.
    
    	function TransformChunk()
    	{
    		ChunkCount ++;
    		XSLProcessor.transform();
    		var chunk = XSLProcessor.output;
    		OutputDiv.innerHTML += "Chunk " + ChunkCount + ":  " + chunk + "<br>";
    	}
    	
    	function ReadyStateChange()
    	{
    		if (XMLDoc.readyState == 4) TransformChunk();
    	}
    </script>
    					
  4. To run the sample, load the HTML document in Internet Explorer (IE) on a computer that has version 2.6 or greater of the Microsoft XML (MSXML) parser installed.

Results

The resulting page displays output each time a chunk of data is retrieved. After all chunks have been retrieved, the entire set of data is displayed. For example, the preceding code displays something similar to the following:

Chunk 1:
Chunk 2: Nanotechnology: 50 Years To Eternity
By: Richard Carey
Cost: $12
.
.
.

This shows that the data was retrieved in more than one chunk.

Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbhowto KB285874