• PHP Video Tutorials

PHP - SimpleXMLElement::__toString() 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 SimpleXMLElement class represents an XML document in PHP.

The SimpleXMLElement::__toString() function retrieves and returns the text content of the current XML element.

Syntax

SimpleXMLElement::__toString();

Parameters

This function doesn’t accept any parameters.

Return Values

This function returns a string value representing the content of the current XML element in case of success and an empty string incase 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 SimpleXMLElement::__toString() function.

<html>
   <head>
      <body>
         <?php
            $str="<?xml version='1.0'?> 
            <text> Welcome to Tutorialspoint</text>";
            $xml=new SimpleXMLElement($str);		 
            $res=$xml->__toString(); 
            print($res);
         ?>      
      </body>
   </head>   
</html> 

This will produce following result −

Welcome to Tutorialspoint

Example

Following example reads the contents of an XML file and prints the names of the elements in 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>
   
   <Tutorial>
      <Name>OpenCV</Name>
      <Pages>150</Pages>
      <Author>Maruti</Author>
      <Version>3.0</Version>
   </Tutorial>
</Tutorials>

sample.html:

<html>
   <head>      
      <body>         
         <?php
            $doc = new DOMDocument;
            $xml = simplexml_load_file("trail.xml");
		      //file to SimpleXMLElement 
            $xml = simplexml_import_dom($xml);
		
            print($xml->getName()."<br>");
            foreach ($xml->children() as $child){
               print("::". $child->getName() ."<br>");
               foreach ($child->children() as $child){
                  print(":::::". $child->getName() ."::".$child->__toString() ."<br>");
               }
            }
         ?>
      </body>
   </head>
</html>

This will produce the following result −

Tutorials
::Tutorial
:::::Name::JavaFX
:::::Pages::535
:::::Author::Krishna
:::::Version::11
::Tutorial
:::::Name::CoffeeScript
:::::Pages::235
:::::Author::Kasyap
:::::Version::2.5.1
::Tutorial
:::::Name::OpenCV
:::::Pages::150
:::::Author::Maruti
:::::Version::3.0

Example

Following is another example of this function −

<html>
   <head>      
      <body>         
         <?php
            $data = "<Tutorials>  </Tutorials>";
            $xml = simplexml_load_string($data);
            print_r($xml);

            //Adding the child node
            $child = $xml->addChild('Tutorial');
            $child->addChild('Name', 'OpenCV');
            $child->addChild('Pages', '230');
            $child->addChild('Author', 'Maruthi');
            $child->addChild('Version', '5.5');
 
            foreach ($xml->children() as $child){
               print("::". $child->getName() ."<br>");
               foreach ($child->children() as $child){
                  print(":::::". $child->getName());
                  print(" -- ". $child->__toString() ."<br>");

               }
            }
         ?>
      </body>
   </head>
</html>

This will produce following result −

SimpleXMLElement Object ( ) ::Tutorial
:::::Name -- OpenCV
:::::Pages -- 230
:::::Author -- Maruthi
:::::Version -- 5.5
php_function_reference.htm
Advertisements