• PHP Video Tutorials

PHP simplexml_load_string() 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_string() accepts an (well-formed) XML string as a parameter converts it into an object of the SimpleXMLElement class and returns it.

Syntax

simplexml_load_string($data, [$class_name, $options, $ns, $is_prefix]);

Parameters

Sr.No Parameter & Description
1

data (Mandatory)

This is a string value representing a XML string which is to be interpreted as an object.

2

class_name(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_string() function.

<html>
   <head>
      <body>
         <?php
            $data="<?xml version='1.0' encoding='UTF-8'?>
            <Employee>
               <Name>Raju</Name>
               <Age>25</Age>
               <Salary>2000</Salary>
            </Employee>";
            $xml = simplexml_load_string($data);
            print_r($xml);
         ?>      
      </body>
   </head>   
</html>

This will produce following result −

SimpleXMLElement Object ( [Name] => Raju [Age] => 25 [Salary] => 2000 ) 

Example

Following is another example of this function, in here we are trying to interpret an XML sting which has multiple records −

<html>
   <head>      
      <body>         
         <?php
            $str="<?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>";
            $xml = simplexml_load_string($str);
            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 −

<html>
   <head>      
      <body>         
         <?php
            $str = "<Tutorial>
            <Name>JavaFX</Name>
            <Pages>535</Pages>
            <Author>Krishna</Author>
            <Version>11</Version>
            </Tutorial>";
            $xml = simplexml_load_string($str, "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

<?php
   $note = <<<XML   
   <note>
      <to>Gopal</to>
      <from>CEO</from>
      <heading>Reminder</heading>
      <body>Don't forget to send a file to me</body>
   </note>
   XML;   
   $xml = simplexml_load_string($note);
   echo $xml->to . "<br>";
   echo $xml->from . "<br>";
   echo $xml->heading . "<br>";
   echo $xml->body;
?>

This will produce following result −

Gopal
CEO
Reminder
Don't forget to send a file to me
php_function_reference.htm
Advertisements