Wednesday, October 14, 2020

PHP - AJAX XML Parser



Hello dear readers! welcome back to another section of my 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 xml from the local directory as well as servers. The following example below illustrates 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 will call the getcourse.php by using the GET method. 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 list of courses and details. This file is accessed by 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 my 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 i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.