• PHP Video Tutorials

PHP - SimpleXMLIterator::getChildren() 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 SimpleXMLIterator is used to iterate all the nodes of an XML document.

The SimpleXMLIterator::getChildren() function returns the sub-elements of the current element of the iterator as an object of the SimpleXMLElement class.

Syntax

SimpleXMLIterator::getChildren(void);

Parameters

This function doesn’t accept any parameters.

Return Values

This function returns a boolean value which is TRUE if the current element of the iterator has sub-elements and, FALSE if not.

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 SimpleXMLIterator:: getChildren() function.

<html>
   <head>
      <body>
         <?php
            $doc=new DOMDocument;
            $data="<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>";
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator($data);
            $xmlIterator->rewind();
            
            //Retrieving the iterator
            $res = $xmlIterator->getChildren();
            print_r($res);
            echo "<br><br>";
            
            //Moving to the next element
            $xmlIterator->next();
            $res = $xmlIterator->getChildren();
            print_r($res);
         ?>      
      </body>
   </head>   
</html> 

This will produce following result −

SimpleXMLIterator Object ( 
   [Name] => JavaFX [Pages] => 535 
   [Author] => Krishna [Version] => 11 
) 
SimpleXMLIterator Object ( 
   [Name] => CoffeeScript [Pages] => 235 
   [Author] => Kasyap [Version] => 2.5.1 
)

Example

In the following example we are trying to retrieve the third record of an XML document −

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;
            
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator("Data.xml", 0, TRUE, "", FALSE);
            $xmlIterator->rewind();
            
            //Retrieving the iterator
            $xmlIterator->next();
            $xmlIterator->next();
            $res = $xmlIterator->getChildren();
            print_r($res);		
         ?>      
      </body>
   </head>   
</html> 

This will produce the following output −

SimpleXMLIterator Object ( 
   [Name] => OpenCV [Pages] => 150 
   [Author] => Maruti [Version] => 3.0 
)

Example

In the following example we are trying to display all the records of an XML file −

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;
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator("data.xml", 0, TRUE, "", FALSE);
            $xmlIterator->rewind();
            //Retrieving the iterator
            //$xmlIterator->next();
            for($xmlIterator->rewind(); $xmlIterator->valid(); 
            $xmlIterator->next() ){
               foreach($xmlIterator->getChildren() as $key => $val) {
                  print($key ."::". $val . "<br>");
               }
               echo "<br>";
            }		
         ?>      
      </body>
   </head>   
</html> 

This will produce the following result −

Name::JavaFX
Pages::535
Author::Krishna
Version::11

Name::CoffeeScript
Pages::235
Author::Kasyap
Version::2.5.1

Name::OpenCV
Pages::150
Author::Maruti
Version::3.0
php_function_reference.htm
Advertisements