• PHP Video Tutorials

PHP SimpleXMLElement::children() 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::attributes() function finds out the attributes along with the values in the SimpleXMLElement object and returns them.

Syntax

SimpleXMLElement::attributes([$namespace, $is_prefix]);

Parameters

Sr.No Parameter & Description
1

namespace(Optional)

This is a string value representing the namespace to which the attribute belongs.

2

Is_prefix(Optional)

This is a boolean value representing whether the specified name space is a prefix (TRUE) or an URL (FALSE).

Return Values

This returns an object of the SimpleXMLElement class representing the children nodes.

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

<html>
   <head>
      <body>
         <?php
            $str="<?xml version='1.0' standalone='yes'?>
            <Tutorial>
               <Name type = 'programming'>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>11</Version>
            </Tutorial>";
            $xml = new SimpleXMLElement($str);
            print("Child nodes: <br>");
            foreach ($xml->children() as $child){
               print($child . "<br>");
            }
         ?>      
      </body>
   </head>   
</html>

This will produce following result −

Child nodes:
JavaFX
535
Krishna
11

Example

Following is another example of this function here, we are trying to get the children in an XML file −

<html>
   <head>      
      <body>         
         <?php
            $doc = new DOMDocument;
            $xml = simplexml_load_file("data.xml");
            //file to SimpleXMLElement 
            $xml = simplexml_import_dom($xml);

            print("Child nodes: <br>");
            foreach ($xml->children() as $child){
                print($child . "<br>");
            }			
         ?>
      </body>
   </head>
</html>

This will produce following result −

Child nodes:
CoffeeScript
235
Kasyap
2.5.1 
php_function_reference.htm
Advertisements