WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

AJAX XML Example

AJAX can be utilized for intuitive correspondence with a XML file.


AJAX XML Example

The following model will show how a site page can get data from a XML record with AJAX:

Example


Example Explained

When an individual taps on the "Get CD info" catch over, the loadDoc() work is executed.

The loadDoc() work makes a XMLHttpRequest object, includes the capacity to be executed when the server reaction is prepared, and sends the demand off to the server.

When the server reaction is prepared, a HTML table is manufactured, hubs (components) are removed from the XML document, and it at long last updates the txtCDInfo placeholder with the HTML table loaded up with XML data:

LoadXMLDoc()

work loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = work() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
    myFunction(xhttp);
    }
  };
  xhttp.open("GET", "cd_catalog.xml", true);
  xhttp.send();
}
function myFunction(xml) {
  var i;
  var xmlDoc = xml.responseXML;
  var table="<tr><th>Artist</th><th>Title</th></tr>";
  var x = xmlDoc.getElementsByTagName("CD");
  for (I = 0; I <x.length; i++) {
    table += "<tr><td>" +
    x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
    "</td></tr>";
  }
  document.getElementById("demo").innerHTML = table;
}

The XML File

The XML record utilized in the model above resembles this: "cd_catalog.xml".