PHP - AJAX XML Parser

PHP | AJAX XML Parser


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we will be discussing about the PHP AJAX XML Parser.

AJAX XML Example

Using PHP with AJAX, we can parse an xml from the local directory and also the server. The following example below demonstrates how to parse xml with browser.

<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:
         <select name = "cds" onchange = "showCD(this.value)">
            <option value = "">Select a course:</option>
            <option value = "Android">CSS</option>
            <option value = "Html">HTML</option>
            <option value = "Java">JavaScript</option>
            <option value = "Microsoft">Python</option>
         </select>
      </form>
      
      <div id = "txtHint"><b>Course info will be listed here...</b></div>
      
   </body>
</html>


The above example calls the getcourse.php using the GET method. The getcourse.php file loads catalog.xml. getcourse.php file is shown below -

<?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

This is the xml file having a list of courses and details. The file can be accessed by the getcourse.php -

<CATALOG>
   <SUBJECT>
      <COURSE>CSS</COURSE>
      <COUNTRY>Nigeria</COUNTRY>
      <COMPANY>Web Design Tutorialz</COMPANY>
      <PRICE>$15</PRICE>
      <YEAR>2018</YEAR>
   </SUBJECT>
   
   <SUBJECT>
      <COURSE>HTML</COURSE>
      <COUNTRY>Nigeria</COUNTRY>
      <COMPANY>Web Design Tutorialz</COMPANY>
      <PRICE>$25</PRICE>
      <YEAR>2017</YEAR>
   </SUBJECT>
   
   <SUBJECT>
      <COURSE>JavaScript</COURSE>
      <COUNTRY>Nigeria</COUNTRY>
      <COMPANY>Web Design Tutorialz</COMPANY>
      <PRICE>$40</PRICE>
      <YEAR>2019</YEAR>
   </SUBJECT>
   
   <SUBJECT>
      <COURSE>Python</COURSE>
      <COUNTRY>Nigeria</COUNTRY>
      <COMPANY>Web Design Tutorialz</COMPANY>
      <PRICE>$50</PRICE>
      <YEAR>2020</YEAR>
   </SUBJECT>
</CATALOG>

Output

When the above code is executed, it will produce the following result -



Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the AJAX Auto Complete Search in PHP.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.