YouTip LogoYouTip

Ajax Xmlfile

# AJAX XML Example * * * AJAX can be used for interactive communication with an XML file. * * * ## AJAX XML Example The following example demonstrates how a webpage can use AJAX to read information from an XML file: ## Example Get My CD Collection [Try it Β»](#) * * * ## Example Explained - The loadXMLDoc() Function When the user clicks on the "Get My CD Collection" button above, the loadXMLDoc() function is executed. The loadXMLDoc() function creates the XMLHttpRequest object, adds a function to be executed when the server response is ready, and sends the request off to the server. When the server response is ready, an HTML table is built, nodes (elements) are extracted from the XML file, and the table is filled with the XML data: ## Asynchronously Load XML Document function loadXMLDoc(){var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){if(this.readyState == 4&&this.status == 200){myFunction(this); }}; xhttp.open("GET", "cd_catalog.xml", true); xhttp.send(); }function myFunction(xml){var i; var xmlDoc = xml.responseXML; var table="ArtistTitle"; var x = xmlDoc.getElementsByTagName("CD"); for(i = 0; i<x.length; i++){table += "" + x.getElementsByTagName("ARTIST").childNodes.nodeValue + "" + x.getElementsByTagName("TITLE").childNodes.nodeValue + ""; }document.getElementById("demo").innerHTML = table; } * * * ## AJAX Server Page The server page used in the example above is actually an XML file called "[cd_catalog.xml](#)". [](#)(#) (#)[](#)
← Dom Nodes AddAjax Database β†’