Skip to content Skip to sidebar Skip to footer

Read XML file using PHP

Reading Time: < 1 minute

Today I’m going to describes how to read xml files using php.I’ll explain this using simple php example.If you have simple knowledge about xml this is very easy job.

Students.xml



  
    Butcher
    French
    [email protected]
    09412345678
  
  
    Smith
    United State
    [email protected]
    094987456258
  
  
    Jack Sparrow
    Carribian
    [email protected]
    094784210308
  

index.php

load("Students.xml");
  $students = $doc->getElementsByTagName("student");
  $i=1;
  foreach( $students as $student )
  {
          // Get user Name
          $names 	= $student->getElementsByTagName("name");
          $Name 	= $names->item(0)->nodeValue;
          // Get user Country
          $countries    = $student->getElementsByTagName("country");
          $Country 	= $countries->item(0)->nodeValue;
          // Get user Email
          $emails 	= $student->getElementsByTagName("email");
          $Email 	= $emails->item(0)->nodeValue;
	  // Get user Phone
          $phones 	= $student->getElementsByTagName("phone");
          $Phone 	= $phones->item(0)->nodeValue;
  	  echo "Student ID - ".$i.'
'; echo "Name - ".$Name.'
'; echo "Country - ".$Country.'
'; echo "Email - ".$Email.'
'; echo "Phone - ".$Phone.'
'; $i++; } ?>

That’s all.
Demo
Download Read XML file using PHP Example(2 KB)