PHP | SimpleXMLElement children() Function

Pre-requisite: Read XML Basics

The SimpleXMLElement::children() function is an inbuilt function in PHP which returns children of a given node in a SimpleXML object.

Syntax:

SimpleXMLElement SimpleXMLElement::children( $namespace, $is_prefix )

Parameter: This function accepts two parameters as mentioned above and described below:

  • $namespace: It is optional parameter. It specifies the XML namespace.
  • $is_prefix: It is boolean parameter which is optional. It is True if $namespace is a prefix and False if $namespace is namespace URL. Its default value is False.

Return Value: This function returns a SimpleXMLElement object.

Note: This function is available on PHP 5.0.1 and newer version.

Below programs illustrate the SimpleXMLElement::children() function in PHP:

Program 1: This program displays the value of child node.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document to $user
$user = <<<XML
<user>
    <username>Geeks123</username>
    <name>GeeksforGeeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <address> 
        Noida, UP, India 
    </address>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Print children node
foreach($xml->children() as $child) {
    echo "Child node: " . $child . "</br>";
}
  
?>

chevron_right


Output:

Child node: Geeks123
Child node: GeeksforGeeks
Child node: +91-XXXXXXXXXX
Child node: Noida, UP, India 

Program 2: This program displays the tag name and its value.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document to $user
$user = <<<XML
<user>
    <username>Geeks123</username>
    <name>GeeksforGeeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <address> 
        Noida, UP, India 
    </address>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Print children node
foreach($xml->children() as $child) {
    echo "Child node: " . $child->getName()
            . " => " . $child . "</br>";
}
  
?>

chevron_right


Output:

Child node: username => Geeks123
Child node: name => GeeksforGeeks
Child node: phone => +91-XXXXXXXXXX
Child node: address => Noida, UP, India 

Program 3: This program retrieving the children attribute name with its value.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document to $user
$user = <<<XML
<user>
    <username font-color="green" 
            font="awsome-fonts" font-size="72px">
        Geeks123
    </username>
      
    <name font-color="blue" font="awsome-fonts"
            font-size="36px">
        GeeksforGeeks
    </name>
      
    <phone font-color="blue" type="number" 
            font="awsome-fonts" font-size="24px">
        +91-XXXXXXXXXX
    </phone>
      
    <address font-color="blue" font="awsome-fonts" 
            font-size="24px"
        Noida, UP, India 
    </address>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Print children attribute
foreach($xml->children() as $child) {
    echo "Child name: " . $child->getName()
            . " =>" . $child . "<br>";
      
    foreach($child->attributes() as $key => $value)
        echo "      parameter: " 
                . $key . " => " . $value . "</br>";
}
  
?>

chevron_right


Output:

Child name: username => Geeks123 
     parameter: font-color => green
     parameter: font => awsome-fonts
     parameter: font-size => 72px
Child name: name => GeeksforGeeks 
     parameter: font-color => blue
     parameter: font => awsome-fonts
     parameter: font-size => 36px
Child name: phone => +91-XXXXXXXXXX 
     parameter: font-color => blue
     parameter: type => number
     parameter: font => awsome-fonts
     parameter: font-size => 24px
Child name: address => Noida, UP, India 
     parameter: font-color => blue
     parameter: font => awsome-fonts
     parameter: font-size => 24px

Reference: https://www.php.net/manual/en/simplexmlelement.children.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.