PHP – AJAX XML Parser

By | October 1, 2021
PHP - AJAX XML Parser

If you want to parse the XML code from the local directory and even from the server then you can use AJAX. below example will allow you to parse XML from the web browser. Check below about PHP – AJAX XML Parser.

PHP – AJAX XML Parser

Example:

<html>
   <head>
      <script>
         function showCD(str) {
            if (str == "") {
               document.getElementById("txtHint").innerHTML = "";
               return;
            }
            if (window.XMLHttpRequest) {
               // code for IE7+, Firefox, Chrome, Opera, Safari
               xmlhttp = new XMLHttpRequest();
            }else {  
               // code for IE6, IE5
               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
               if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                  document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
               }
            }
            xmlhttp.open("GET","getcourse.php?q="+str,true);
            xmlhttp.send();
         }
      </script>
   </head>
   <body>
      <form>
         Select a Course From the List:
         <select name = "cds" onchange = "showCD(this.value)">
            <option value = "">Select prefered Subject:</option>
            <option value = "Chemistry">Chemistry </option>
            <option value = "Maths">Maths</option>
            <option value = "Physcis">Physcis</option>
         </select>
      </form>      
      <div id = "txtHint"><b>Subjects details are listed below...</b></div>
   </body>
</html>

As you have noticed above code is calling getcourse.php script with the help of the GET method which is further loading the catalog.xml file.

Below is the script for getcourse.php.

getcourse.php

Vamware
<?php
   $q = $_GET["q"];
   $xmlDoc = new DOMDocument();
   $xmlDoc->load("catalog.xml");
   $x = $xmlDoc->getElementsByTagName('COURSE');
   for ($i = 0; $i<=$x->length-1; $i++) {
      =
      if ($x->item($i)->nodeType == 1) {
         if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
            $y = ($x->item($i)->parentNode);
         }
      }
   }
   $cd = ($y->childNodes);
   for ($i = 0;$i<$cd->length;$i++) {
      if ($cd->item($i)->nodeType == 1) {
         echo("<b>" . $cd->item($i)->nodeName . ":</b> ");
         echo($cd->item($i)->childNodes->item(0)->nodeValue);
         echo("<br>");
      }
   }
?>

catalog.xml file

<CATALOG>
   <SUBJECT>
      <COURSE>Chemistry</COURSE>
      <COUNTRY>India</COUNTRY>
      <COMPANY>Techgeekbuzz</COMPANY>
      <PRICE>$10</PRICE>
      <YEAR>2015</YEAR>
   </SUBJECT>
   <SUBJECT>
      <COURSE>Maths</COURSE>
      <COUNTRY>India</COUNTRY>
      <COMPANY>Techgeekbuzz</COMPANY>
      <PRICE>$15</PRICE>
      <YEAR>2015</YEAR>
   </SUBJECT>
   <SUBJECT>
      <COURSE>Physics</COURSE>
      <COUNTRY>India</COUNTRY>
     <COMPANY>Techgeekbuzz</COMPANY>
      <PRICE>$20</PRICE>
      <YEAR>2015</YEAR>
   </SUBJECT>
</CATALOG>

People are also reading:

Vamware

Leave a Reply

Your email address will not be published. Required fields are marked *