🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #259

AJAX XML File


AJAX can be used for interactive communication with an XML file.


AJAX XML Example

The following example will demonstrate how a web page can fetch information from an XML file with AJAX:

Example

<div id="demo">
</div>

Example Explained

When a user clicks on the "Get CD info" button above, the loadDoc() function is executed.

The loadDoc() function creates an XMLHttpRequest object, adds the 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 it finally updates the element "demo" with the HTML table filled with XML data:

function loadDoc() {

  const xhttp = new XMLHttpRequest();

  xhttp.onload = function() {myFunction(this);}

  xhttp.open("GET", "cd_catalog.xml");

  xhttp.send();

}

function myFunction(xml) {

  const xmlDoc = xml.responseXML;

  const x = xmlDoc.getElementsByTagName("CD");

  let table="<tr><th>Artist</th><th>Title</th></tr>";

  for (let 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 file used in the example above looks like this: "cd_catalog.xml".

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

AJAX XML File – FAQs

Quick answers about learning AJAX XML File in JavaScript.

This free note from CodingNow 2.0 explains AJAX XML File in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including AJAX XML File, is 100% free with no signup required.
With focused practice, most students grasp AJAX XML File in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now