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.



     <?xml version="1.0" encoding="utf-8"?>
        <movie id ="sean">
                <name>Dr no</name>
         <movie id ="mathew">
                <name>Dr yes</name>
        <movie id ="roger">
         <movie id ="sean">
        <movie id ="sean">
       <movie id ="george">
                <name>Winter Break </name>
        <movie id ="sean">
                <name>Living Daylights</name>
                <etime>23:00 </etime>
        <movie id ="daniel">
                <name> Skyfall </name>
                <stime> 11:00 </stime>
                <etime> 12:00 </etime>
        <movie id ="daniel">
                <name> Casino Royale </name>
                <stime> 12:00 </stime>
                <etime> 14:00 </etime>
        <movie id ="pierce"> 
                <name> GoldenEye  </name>
                <stime> 11:00 </stime>
                <etime> 13:00 </etime>


Syntax and usage of SimpleXMLElement class



    SimpleXMLElement implements Traversable {
    /* Methods */
    final public __construct ( string $data [, int $options = 0 [, bool $data_is_url = false [,string $ns = "" [, bool $is_prefix = false ]]]] )
    public void addAttribute ( string $name [, string $value [, string $namespace ]] )
    public SimpleXMLElement addChild ( string $name [, string $value [, string $namespace ]] )
    public mixed asXML ([ string $filename ] )
    public SimpleXMLElement attributes ([ string $ns = NULL [, bool $is_prefix = false ]] )
    public SimpleXMLElement children ([ string $ns [, bool $is_prefix = false ]] )
    public int count ( void )
    public array getDocNamespaces ([ bool $recursive = false [, bool $from_root = true ]] )
    public string getName ( void )
    public array getNamespaces ([ bool $recursive = false ] )
    public bool registerXPathNamespace ( string $prefix , string $ns )
    public string __toString ( void )
    public array xpath ( string $path )
      $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 )



    require ('class.tvSchedule.php');
    $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.



      $movies = new SimpleXMLElement($this->xmlString);
                foreach ($movies->movie as $element) {
                //set channel name for current listing
                $channel = strval($element['id'][0]);
                   $name = "";
                   $etime ="";
                   $stime ="";
                //echo $channel;
                  foreach($element as $key => $val) {
                   //assign variables for use in building listings array.
                   if($key == 'channel'){
                      $channel = strval($val);
                   } if($key == 'name'){
                  // echo "booo";
                      $name = strval($val);
                   } if($key == 'etime'){
                      $etime = strval($val);
                      //echo $etime;
                   }if($key == 'stime'){
                      $stime = strval($val);
                  // echo str_replace(" ", "_", $name);
                   //Add listing to correct channel removing white space within the listing name and replacing with "_"
                   $chan[$channel][str_replace(" ", "_", $name)]['name'] = $name;
                   $chan[$channel][str_replace(" ", "_", $name)]['etime'] = $etime;
                   $chan[$channel][str_replace(" ", "_", $name)]['stime'] = $stime;
                   //as a by product a empty element was created remove it.



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



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


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



Learn How to install Read XML File v1 PHP Script?

Find More related Read XML File v1 PHP Script

Have any Question or Comment?

Leave a Reply

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