We now have a youtube channel. Subscribe!

PHP XML SAX Parser

PHP XML SAX Parser


Hello folks! welcome back to a new section of our tutorial on PHP. In this section of our PHP tutorial, we will be studying about the PHP XML SAX Parser.

SAX parser is used to parse xml files and it is better for memory management than the simple xml parser and the xml Dom parser. It does not retain any data in memory, so it can be used for very large files.

Example

The below example shows how to get data from xml using SAX API.

SAX.xml

SAX.xml should be as follows -

<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
   <course>
      <name>Android</name>
      <country>Nigeria</country>
      <email>contact@webdesigntutorialz.com</email>
      <phone>1234567788990</phone>
   </course>
   
   <course>
      <name>JavaScript</name>
      <country>Nigeria</country>
      <email>contact@webdesigntutorialz.com</email>
      <phone>1234567788990</phone>
   </course>
   
   <course>
      <name>WorldPress</name>
      <country>Nigeria</country>
      <email>contact@webdesigntutorialz.com</email>
      <phone>1234567788990</phone>
   </course>
</tutors>


SAX.php

PHP file should be as follows -

<?php
   //Reading XML using the SAX(Simple API for XML) parser 
   
   $tutors   = array();
   $elements   = null;
   
   // Called to this function when tags are opened 
   function startElements($parser, $name, $attrs) {
      global $tutors, $elements;
      
      if(!empty($name)) {
         if ($name == 'COURSE') {
            // creating an array to store information
            $tutors []= array();
         }
         $elements = $name;
      }
   }
   
   // Called to this function when tags are closed 
   function endElements($parser, $name) {
      global $elements;
      
      if(!empty($name)) {
         $elements = null;
      }
   }
   
   // Called on the text between the start and end of the tags
   function characterData($parser, $data) {
      global $tutors, $elements;
      
      if(!empty($data)) {
         if ($elements == 'NAME' || $elements == 'COUNTRY' ||  $elements == 'EMAIL' ||  $elements == 'PHONE') {
            $tutors[count($tutors)-1][$elements] = trim($data);
         }
      }
   }
   
   // Creates a new XML parser and returns a resource handle referencing it to be used by the other XML functions. 
   $parser = xml_parser_create(); 
   
   xml_set_element_handler($parser, "startElements", "endElements");
   xml_set_character_data_handler($parser, "characterData");
   
   // open xml file
   if (!($handle = fopen('sax.xml', "r"))) {
      die("could not open XML input");
   }
   
   while($data = fread($handle, 4096)) // read xml file {
      xml_parse($parser, $data);  // start parsing an xml document 
   }
   
   xml_parser_free($parser); // deletes the parser
   $i = 1;
   
   foreach($tutors as $course) {
      echo "course No - ".$i.'<br/>';
      echo "course Name - ".$course['NAME'].'<br/>';
      echo "Country - ".$course['COUNTRY'].'<br/>';
      echo "Email - ".$course['EMAIL'].'<br/>';
      echo "Phone - ".$course['PHONE'].'<hr/>'; 
      $i++; 
   }
?>

Output

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



Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about PHP - Dom Parser.

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.

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.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain