Microsoft KB Archive/285874

= How To Retrieve XML Data Asynchronously with ondataavailable Event =

Article ID: 285874

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Core Services 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(&quot;MSXML2.FreeThreadedDOMDocument.4.0&quot;);
 * 2) XMLDoc = new ActiveXObject(&quot;MSXML2.DOMDocument.4.0&quot;);
 * 3) XSLTemplate = new ActiveXObject(&quot;MSXML2.XSLTemplate.4.0&quot;);

  Paste the following XML code into a new document and save the document as books.xml:   Nanotechnology: 50 Years To Eternity Richard Carey Nobel Prize for Scientific Literature 12     Reid Wilkes: The Man, The Myth William Shatner 55                    </li> <li> 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=&quot;xml&quot; indent=&quot;yes&quot; omit-xml-declaration=&quot;yes&quot;/> <xsl:template match=&quot;/&quot;> <xsl:for-each select=&quot;bookstore/book&quot;> <xsl:value-of select=&quot;title&quot;/> By: <xsl:value-of select=&quot;author&quot;/> Cost: $<xsl:value-of select=&quot;price&quot;/> </xsl:for-each> </xsl:template> </xsl:stylesheet> </li> <li> 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: <body onload=&quot;AsyncLoadXML('http://MyServer/books.xml', 'http://MyServer/books.xsl');&quot;> Output: <hr width=&quot;100%&quot;> <div id=&quot;OutputDiv&quot;/>

<script language=&quot;JavaScript&quot;> 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(&quot;MSXML2.FreeThreadedDOMDocument&quot;); XSLDoc.async = false; XSLDoc.load(XSLPath); // Initialize the XML document... XMLDoc = new ActiveXObject(&quot;MSXML2.DOMDocument&quot;); XMLDoc.ondataavailable = TransformChunk; XMLDoc.onreadystatechange = ReadyStateChange; XMLDoc.async = true; // Initialize the XSLTemplate document... XSLTemplate = new ActiveXObject(&quot;MSXML2.XSLTemplate&quot;); 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 += &quot;Chunk &quot; + ChunkCount + &quot;: &quot; + chunk + &quot; &quot;; }   function ReadyStateChange {       if (XMLDoc.readyState == 4) TransformChunk; }                   </li> <li>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.</li></ol>

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.

Keywords: kbhowto KB285874

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.