How to fetch information from an XML file with AJAX?

Asked 19-Aug-2021
Viewed 451 times

1 Answer



<!doctype html>

<html lang='en'>
    <!-- Required meta tags -->
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <!-- Bootstrap CSS -->
    <link href='' rel='stylesheet' integrity='sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC' crossorigin='anonymous'>
    <title>Ajax Example</title>
    <div class='container mt-3'>
        <h1 class='text-center'>Ajax Example </h1>
        <button type='button' onclick='loadResponseXML()' class='btn btn-primary my-3'>Click me for see the details of selected xml file</button>
        <h3 class='text-danger '>See details of of page</h3>
        <p class='text-primary'>Whole information of xml file <span id='details' class='text-muted'></span></p>
        <h2>Book table here !!!</h2>
        <table id='loadDataInTable' class='table table-striped table-hover'></table>
        function loadResponseXML() {
            // creating instance of XMLHttpRequest()
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById('details').innerHTML = xhttp.getAllResponseHeaders();
                    // call responseXML
                    var xmlDoc = xhttp.responseXML;
                    var x = xmlDoc.getElementsByTagName('BOOK');
                    let table='<thead class=\'thead-dark\'><tr><th> # </th><th>TITLE of Books</th><th>PRICE</th></tr></thead>';
                    for (let i = 0; i <x.length; i++) {
                        table += '<tr><td> '+(i+1)+' </td><td>' +
                        x[i].getElementsByTagName('TITLE')[0].childNodes[0].nodeValue +
                        '</td><td>' +
                        x[i].getElementsByTagName('PRICE')[0].childNodes[0].nodeValue +
                    document.getElementById('loadDataInTable').innerHTML = table;
  'GET', 'xmlfile.xml', true);
    <script src='' integrity='sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM' crossorigin='anonymous'></script>


<?xml version='1.0' encoding='UTF-8'?>

      <AUTHOR>Gambardella, Matthew</AUTHOR>
      <TITLE>XML Developer's Guide</TITLE>
      <DESCRIPTION>An in-depth look at creating applications
      with XML.</DESCRIPTION>
      <AUTHOR>Ralls, Kim</AUTHOR>
      <TITLE>Midnight Rain</TITLE>
      <DESCRIPTION>A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.</DESCRIPTION>
      <AUTHOR>Corets, Eva</AUTHOR>
      <TITLE>Maeve Ascendant</TITLE>
      <DESCRIPTION>After the collapse of a nanotechnology
      society in England, the young survivors lay the
      foundation for a new society.</DESCRIPTION>
      <AUTHOR>Corets, Eva</AUTHOR>
      <TITLE>Oberon's Legacy</TITLE>
      <DESCRIPTION>In post-apocalypse England, the mysterious
      agent known only as Oberon helps to create a new life
      for the inhabitants of London. Sequel to Maeve
      <AUTHOR>Corets, Eva</AUTHOR>
      <TITLE>The Sundered Grail</TITLE>
      <DESCRIPTION>The two daughters of Maeve, half-sisters,
      battle one another for control of England. Sequel to
      Oberon's Legacy.</DESCRIPTION>


How to fetch information from an XML file with AJAX?