Google Ads

Read XML File v1 PHP Script


Read XML File v1 PHP Script

Most of web applications handling lot of different kind of data sources in order to provide excellent service to its visitors. Application need properly define and fast feeding data sources to maintain quality of service. XML is one of leading data source which is used to provide various kind of services such as webservice, RESETFull service, RSS feeds and etc. In this tutorial I will teach you how to read XML file using php.

XML files are created with well define tree structure so it can be easily read and interpreted. Major programming language introduce XML file handling functionality for programming convenience. In this tutorial you are going to learn SimpleXMLElement class which is introduce after PHP 5..0.

Sample program contained channel.xml file which has TV schedule list for a day.

 

 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <moviedata>
  3. <movie id ="sean">
  4. <name>Dr no</name>
  5. <stime>09:00</stime>
  6. <etime>11:00</etime>
  7. </movie>
  8. <movie id ="mathew">
  9. <name>Dr yes</name>
  10. <stime>09:00</stime>
  11. <etime>11:00</etime>
  12. </movie>
  13. <movie id ="roger">
  14. <name>Moonraker</name>
  15. <stime>13:00</stime>
  16. <etime>14:00</etime>
  17. </movie>
  18. <movie id ="sean">
  19. <name>Thunderball</name>
  20. <stime>01:00</stime>
  21. <etime>02:30</etime>
  22. </movie>
  23. <movie id ="sean">
  24. <name>Thunderballl</name>
  25. <stime>02:35</stime>
  26. <etime>04:30</etime>
  27. </movie>
  28. <movie id ="george">
  29. <name>Winter Break </name>
  30. <stime>22:00</stime>
  31. <etime>23:00</etime>
  32. </movie>      
  33. <movie id ="sean">
  34. <name>Living Daylights</name>
  35. <stime>22:00</stime>
  36. <etime>23:00 </etime>
  37. </movie>
  38. <movie id ="daniel">
  39. <name> Skyfall </name>
  40. <stime> 11:00 </stime>
  41. <etime> 12:00 </etime>
  42. </movie>
  43. <movie id ="daniel">
  44. <name> Casino Royale </name>
  45. <stime> 12:00 </stime>
  46. <etime> 14:00 </etime>
  47. </movie>
  48. <movie id ="pierce">
  49. <name> GoldenEye </name>
  50. <stime> 11:00 </stime>
  51. <etime> 13:00 </etime>
  52. </movie>
  53. </moviedata>

 

Syntax and usage of SimpleXMLElement class

 

 
 
  1. SimpleXMLElement implements Traversable {
  2. /* Methods */
  3. final public __construct ( string $data [, int $options = 0 [, bool $data_is_url = false [,string $ns = "" [, bool $is_prefix = false ]]]] )
  4. public void addAttribute ( string $name [, string $value [, string $namespace ]] )
  5. public SimpleXMLElement addChild ( string $name [, string $value [, string $namespace ]] )
  6. public mixed asXML ([ string $filename ] )
  7. public SimpleXMLElement attributes ([ string $ns = NULL [, bool $is_prefix = false ]] )
  8. public SimpleXMLElement children ([ string $ns [, bool $is_prefix = false ]] )
  9. public int count ( void )
  10. public array getDocNamespaces ([ bool $recursive = false [, bool $from_root = true ]] )
  11. public string getName ( void )
  12. public array getNamespaces ([ bool $recursive = false ] )
  13. public bool registerXPathNamespace ( string $prefix , string $ns )
  14. public string __toString ( void )
  15. public array xpath ( string $path )
  16. }
  17. $movies = new SimpleXMLElement($this->xmlString);

 

Our sample program has class called class.tvSchedule.php which provides us required functionality to handle xml file operations. At first we need to create instance of above class and pass channel.xml file as a parameter to class constructor. (inside index.php )

 

 
 
  1. require ('class.tvSchedule.php');
  2. $tvSchedule = new tvSchedule(file_get_contents('Channels.xml'));

 

You must understand those xml elements are map to object, so by iterating it we can access any of the elements. Also you should remember that function return xml as array object so we can access its element using its index or using for each statement as a key/value pair. Our file reading method as follows.

 

 
 
  1. $movies = new SimpleXMLElement($this->xmlString);
  2.             foreach ($movies->movie as $element) {
  3.             //set channel name for current listing
  4.             //print_r($element);
  5.             $channel = strval($element['id'][0]);
  6.              $name = "";
  7.              $etime ="";
  8.              $stime ="";
  9.             //echo $channel;
  10.              foreach($element as $key => $val) {
  11.              //assign variables for use in building listings array.
  12.              if($key == 'channel'){
  13.                   $channel = strval($val);
  14.              } if($key == 'name'){
  15.              // echo "booo";
  16.                   $name = strval($val);
  17.              } if($key == 'etime'){
  18.                   $etime = strval($val);
  19.                   //echo $etime;
  20.              }if($key == 'stime'){
  21.                   $stime = strval($val);
  22.              }
  23.              // echo str_replace(" ", "_", $name);
  24.              //Add listing to correct channel removing white space within the listing name and replacing with "_"
  25.              $chan[$channel][str_replace(" ", "_", $name)]['name'] = $name;
  26.              $chan[$channel][str_replace(" ", "_", $name)]['etime'] = $etime;
  27.              $chan[$channel][str_replace(" ", "_", $name)]['stime'] = $stime;
  28.              //as a by product a empty element was created remove it.
  29.              }
  30.             }

 

 

Then you need to call getChannelList() function in order to retrieve channel data

 

 
 
  1.       $list = $tvSchedule->getChannelList();
  2.                   foreach($list as $key => $value){
  3.                         echo '<a href="#" class="list-group-item">'.$value.' </a>';
  4.                   }

 

SimpleXMLElement is simplified the xml file handling process and provide lot of fashion to developer.

 

 

Have any Question or Comment?

Leave a Reply

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

"