You are here: Reference > JavaScript > client-side > xml handling > methods > load (XMLDocument)

load method (XMLDocument)

Browser support:
Downloads the specified XML file and builds an XMLDocument object from it.
The load method resets the XMLDocument object first (clears the document represented by the XMLDocument object, sets the readyState property to one, etc.) and starts the downloading process. The downloading may be handled synchronously or asynchronously (see the async property).
  • In case of synchronous loading, the load method does not return until the downloading ends. After the load method returns and the returned value is true, you can start work with the downloaded XML file. See Example 1 for details.
  • In case of asynchronous loading, the load method starts the downloading in the background and returns. You must register an event handler to be informed about the state of the downloading. In Internet Explorer, use the onreadystatechange property, in Firefox, Opera, Google Chrome and Safari, use the addEventListener method to register the event handler for the 'load' event. See Example 2 for details.
The load method is not supported by Google Chrome and Safari. If you need a cross-browser solution for downloading and opening XML files, use the XMLHttpRequest object. The open method of the XMLHttpRequest object is similar to the load method.

Syntax:

object.load (URL);
You can find the related objects in the Supported by objects section below.

Parameters:

URL
String that specifies the URL for the XML file to load.

Return value:

Boolean. One of the following values:
false The loading failed.
true The loading was successful.

Example HTML code 1:

This example shows how to use an external XML file to insert data into a table:
Code
ajax.js
news.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        function FillTable () {
            var xmlDoc = CreateXMLDocumentObject ();    // defined in ajax.js
            if (!xmlDoc) {
                return;
            }

            if (typeof (xmlDoc.load) === "undefined") { // Google Chrome and Safari
                alert ("Your browser does not support the load method for XML documents!");
                return;
            }

            var url = "news.xml";
            xmlDoc.async = false;
            
            if (xmlDoc.load (url)) {
                var resTable = document.getElementById ("resTable");
                var xmlNodes = ["title", "description", "pubDate", "link"];

                var itemTags = xmlDoc.getElementsByTagName ("item");
                for (i = 0; i < itemTags.length; i++) {
                    resTable.insertRow (i);
                    for (j = 0; j < xmlNodes.length; j++) {
                        var recordNode = itemTags[i].getElementsByTagName (xmlNodes[j])[0];
                        resTable.rows[i].insertCell (j);
                        if ('textContent' in recordNode)
                            resTable.rows[i].cells[j].innerHTML = recordNode.textContent;
                        else
                            resTable.rows[i].cells[j].innerHTML = recordNode.text;
                    }
                }
            }
            else {
                    // display error message
                var errorMsg = null;
                if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
                    errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
                              + " at line " + xmlDoc.parseError.line
                              + " at position " + xmlDoc.parseError.linepos;
                }
                else {
                    if (xmlDoc.documentElement) {
                        if (xmlDoc.documentElement.nodeName == "parsererror") {
                            errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
                        }
                    }
                }
                if (errorMsg) {
                    alert (errorMsg);
                }
                else {
                    alert ("There was an error while loading XML file!");
                }
            }
        }
    </script>
</head>
<body onload="FillTable ()">
    <table border="1px">
        <thead style="font-weight: bold;">
            <tr>
                <td>Title</td>
                <td>Description</td>
                <td>PubDate</td>
                <td>Link</td>
            </tr>
        </thead>
        <tbody id="resTable">
        </tbody>
    </table>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example is the same as the previous, but it demonstrates asynchronous loading:
Code
ajax.js
news.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        var xmlDoc = null;

        function LoadXML () {
            xmlDoc = CreateXMLDocumentObject ();    // defined in ajax.js
            if (!xmlDoc) {
                return;
            }

            if (typeof (xmlDoc.load) === "undefined") { // Google Chrome and Safari
                alert ("Your browser does not support the load method for XML documents!");
                return;
            }

            var url = "news.xml";
            xmlDoc.async = true;
            if (xmlDoc.addEventListener) {  // all browsers except IE
                xmlDoc.addEventListener("load", OnLoadXML, false);
            }
            else {  // IE
                xmlDoc.onreadystatechange = OnStateChange;
            }
            xmlDoc.load (url);
        }

        function OnLoadXML () {
            FillTable ();
        }

        function OnStateChange () {
            if (xmlDoc.readyState == 0 || xmlDoc.readyState == 4) {
                FillTable ();
            }
        }

        function FillTable () {
            var errorMsg = null;
            if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
                errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
                          + " at line " + xmlDoc.parseError.line
                          + " at position " + xmlDoc.parseError.linepos;
            }
            else {
                if (xmlDoc.documentElement) {
                    if (xmlDoc.documentElement.nodeName == "parsererror") {
                        errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
                    }
                }
            }
            if (errorMsg) {
                alert (errorMsg);
                return null;
            }

            var resTable = document.getElementById ("resTable");
            var xmlNodes = ["title", "description", "pubDate", "link"];

            var itemTags = xmlDoc.getElementsByTagName ("item");
            for (i = 0; i < itemTags.length; i++) {
                resTable.insertRow (i);
                for (j = 0; j < xmlNodes.length; j++) {
                    var recordNode = itemTags[i].getElementsByTagName (xmlNodes[j])[0];
                    resTable.rows[i].insertCell (j);
                    if ('textContent' in recordNode)
                        resTable.rows[i].cells[j].innerHTML = recordNode.textContent;
                    else
                        resTable.rows[i].cells[j].innerHTML = recordNode.text;
                }
            }
        }
    </script>
</head>
<body onload="LoadXML ()">
    <table border="1px">
        <thead style="font-weight: bold;">
            <tr>
                <td>Title</td>
                <td>Description</td>
                <td>PubDate</td>
                <td>Link</td>
            </tr>
        </thead>
        <tbody id="resTable">
        </tbody>
    </table>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content