PHP - simplexml_load_file() Function

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The simple XML parser is used to parse Name, attributes and textual content.
The simplexml_load_file() accepts the absolute path of an XML file as a parameter, converts it into an object of the SimpleXMLElement class and returns it.
simplexml_load_file($filename, [$class_name, $options, $ns, $is_prefix]);

filename (Mandatory)
This is a string value representing the name/path of an XML file.
time(Optional)
This is a string value to representing the name of the class (sub class of the SimpleXMLElement).
If you pass this value, the given XML string is returned as the object of the specified class.
optional(Optional)
This is an integer value used to specify the additional Libxml parameters.
ns(Optional)
This is a string value representing the namespace prefix or URI.
Is_prefix(Optional)
This is a boolean value representing whether the previous option is a prefix or an URI.
This function returns an object of the SimpleXMLElement class in case of success and returns the boolean value FALSE in case of failure.
This function was first introduced in PHP Version 5 and works in all the later versions.
Following example demonstrates the usage of the simplexml_load_file() function.
data.xml:
<Tutorial>
   <Name>JavaFX</Name>
   <Pages>535</Pages>
   <Author>Krishna</Author>
   <Version>11</Version>
</Tutorial>
sample.html:
<html>
   <head>      
      <body>         
         <?php
            $xml = simplexml_load_file("data.xml");
            print("<br>");
            print_r($xml);
         ?>
      </body>
   </head>
</html>
This will produce following result −
SimpleXMLElement Object ( 
   [Name] => JavaFX [Pages] => 535 
   [Author] => Krishna [Version] => 11 
)

In the following example we are trying to load an XML file with multiple records and retrieve values from it −
Data.xml:
<?xml version="1.0" encoding="utf-8"?>
<Tutorials>
   <Tutorial>
      <Name>JavaFX</Name>
      <Pages>535</Pages>
      <Author>Krishna</Author>
      <Version>11</Version>
   </Tutorial>
   <Tutorial>
      <Name>CoffeeScript</Name>
      <Pages>235</Pages>
      <Author>Kasyap</Author>
      <Version>2.5.1</Version>
   </Tutorial>
</Tutorials>
sample.html
<html>
   <head>      
      <body>         
         <?php
            $xml = simplexml_load_file("mydata.xml");
            print("<br>");
            foreach($xml->children() as $tut) {
               print($tut->Name ."<br>");
               print($tut->Pages ."<br>");
               print($tut->Author ."<br>");
               print($tut->Version ."<br>");
               print("<br>");
            }
         ?>
      </body>
   </head>
</html>

This will produce the following output −
JavaFX
535
Krishna
11

CoffeeScript
235
Kasyap
2.5.1
Following example demonstrates the usage of this method with options −
<Tutorial>
   <Name>JavaFX</Name>
   <Pages>535</Pages>
   <Author>Krishna</Author>
   <Version>11</Version>
</Tutorial>
sample.html
<html>
   <head>      
      <body>         
         <?php
            $xml = simplexml_load_file("data.xml", "SimpleXMLElement", LIBXML_BIGLINES, FALSE);
            print("<br>");
            print($xml->Name ."<br>");
            print($xml->Pages ."<br>");
            print($xml->Author ."<br>");
            print($xml->Version);
         ?>
      </body>
   </head>
</html>
This will produce following result −
JavaFX
535
Krishna
11
Assume we have a file with name sample.xml as shown below −
data.xml
<note>
   <to>Gopal</to>
   <from>CEO</from>
   <heading>Reminder</heading>
   <body>Don't forget to send a file to me</body>
</note>
Following example loads the above file −
<?php
   $xml = simplexml_load_file("sample.xml");
   print_r($xml);
?>
This will produce the following result −
SimpleXMLElement Object (
   [to] => gopal [from] => CEO 
   [heading] => Reminder [body] => Don't forget to send a file to me 
)


    45 Lectures 
     9 hours 

 Malhar Lathkar

    34 Lectures 
     4 hours 

 Syed Raza

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    17 Lectures 
     1 hours 

 Nivedita Jain

    100 Lectures 
     34 hours 

 Azaz Patel

    43 Lectures 
     5.5 hours 

 Vijay Kumar Parvatha Reddy
 Print
 Add Notes
 Bookmark this page