Web Services, Part VI: XML Parsing and Loading from JavaScript: Monitoring the XML Parsing and Loading Process - Doc JavaScript | WebReference

Web Services, Part VI: XML Parsing and Loading from JavaScript: Monitoring the XML Parsing and Loading Process - Doc JavaScript


Web Services, Part VI: XML Parsing and Loading from JavaScript

Monitoring the XML Parsing and Loading Process

The DOMDocument object has a very extensive set of properties, methods, and events to deal with reading XML files and loading their data in. The readyState property can give direct feedback on the reading processes. Here are the possible values for readyState:

StateShort DescriptionLong Description
1LoadingPreparing to read the XML file. Did not try yet.
2LoadedReading and parsing the XML file. Object model still not available.
3InteractivePart of the XML file successfully parsed and read in. Object model partially available for read only.
4CompletedLoading of the XML file has been completed, successfully or unsuccessfully.

When you read an XML file successfully, the readyState property goes through all four states: 1,2,3, and 4. When you are unsuccessful in reading an XML file, the readyState property may skip 3 on some operating systems. Obviously, it is very difficult to know exactly when the readyState property changes its value. For this very purpose the DOMDocument object provides the onreadystatechange event handler. We incorporated this event handler in the following box to load an XML file. Type mydvd.xml in the input box and see the readyState property goes through all four values. Type an incorrect name, and see state 3 missing from the list (on some systems). Observe the error message:

URL:

The HTML lines that implement this input box are the following:

URL: <INPUT TYPE="text" SIZE="60" ID="url">
<INPUT TYPE="button" VALUE="Load URL"
  onclick="JavaScript:load()">
<DIV ID="results" STYLE="color:red;font-weight:bold;"></DIV>

When the user clicks the Load URL button, the load() function is called:

function load() {
  xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
  xmldoc.onreadystatechange = checkState;
  xmldoc.load(url.value);
}

We create the DOMDocument object, xmldoc, and then assign checkState() as the onreadystatechange event handler. Then we load the URL entered by the user, url.value. From now on, every change in the readyState property will go through the function checkState() and be recorded. Here is the function checkState():

function checkState() {
  var state = xmldoc.readyState;
  results.innerHTML += "readyState = " + state + "<BR>"
  if (state == 4) {
    var err = xmldoc.parseError;
    if (err.errorCode != 0) {
      results.innerHTML += err.reason + "<BR>"
	} else {
	    results.innerHTML +="success" + "<BR>"
      }
  }
}

Notice that the main task of this function is recording the event itself:

   results.innerHTML += "readyState = " + state + "<BR>"

The rest of the function deals with the highest state number, 4. It checks to see if there were errors during the XML loading or if the loading was successful. We'll cover error handling on Page 5.

By default, when you ask JavaScript to load an XML file, the browser will not wait for the loading to complete and will continue to run the next line of the script. You can ask the browser to wait for the loading to complete by assigning the async property to false. Here is a script segment that does it:

xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmldoc.async = false;

Next: How to report errors during XML parsing

https://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: January 14, 2002
Revised: January 14, 2002

URL: https://www.webreference.com/js/column101/4.html