• PHP Video Tutorials

PHP - simplexml_load_file() Function



Definition and Usage

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.

Syntax

simplexml_load_file($filename, [$class_name, $options, $ns, $is_prefix]);

Parameters

Sr.No Parameter & Description
1

filename (Mandatory)

This is a string value representing the name/path of an XML file.

2

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.

3

optional(Optional)

This is an integer value used to specify the additional Libxml parameters.

4

ns(Optional)

This is a string value representing the namespace prefix or URI.

5

Is_prefix(Optional)

This is a boolean value representing whether the previous option is a prefix or an URI.

Return Values

This function returns an object of the SimpleXMLElement class in case of success and returns the boolean value FALSE in case of failure.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

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 
)

Example

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

Example

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

Example

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 
)
php_function_reference.htm
Advertisements