PHP SimpleXMLElement::getChildren() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::getChildren() returns the child elements of the element that the iterator currently points to.

It belongs to the RecursiveIterator interface that SimpleXMLElement implements in PHP 8. Together with hasChildren(), it lets you walk a tree of nested elements. To read children in normal code, use foreach or the children() method instead.

Syntax

Call SimpleXMLElement::getChildren() with the values you need. Start with this line.

PHP
$children = $xml->getChildren();
CallWhat it does
$xml->getChildren()Returns the element at the current iterator position, ready to be looped over to reach its own children.
$xml->hasChildren()Checks first whether the current element has child elements.
$xml->children()The normal way to get the child elements of an element.

getChildren() works on the position of the iterator, so call rewind() and valid() first. Always check hasChildren() before calling getChildren(). If you only need the children of a known element, $xml->children() or foreach is simpler.

Library declarations
PHP
SimpleXMLElement::getChildren(): ?SimpleXMLElement

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
SimpleXMLElementThe element at the current position. Looping over it gives its own child elements.
nullReturned when the iterator does not point to an element.

PHP Examples

Example 1 — Get the Children of the Current Element

Point the iterator at the first book, then loop over the children of that book.

PHP
<?php
$xml = simplexml_load_string(
    '<library><book><title>PHP</title><author>Sam</author></book><note>Hello</note></library>'
);

$xml->rewind();

$children = $xml->getChildren();
foreach ($children as $name => $child) {
    echo $name, ': ', $child, PHP_EOL;
}

How It Works

  1. The library root has a book element with two children, and a note element.
  2. rewind() points the iterator to the first child of library, which is the book element.
  3. getChildren() returns that book element, ready to be looped over to read its own children.
  4. foreach prints the name and text of each child of the book.

Example 2 — Check hasChildren() First

The note element has only text and no child elements, so it should not be walked into.

PHP
<?php
$xml = simplexml_load_string(
    '<library><book><title>PHP</title></book><note>Hello</note></library>'
);

for ($xml->rewind(); $xml->valid(); $xml->next()) {
    if ($xml->hasChildren()) {
        echo $xml->key(), ' has children', PHP_EOL;
        foreach ($xml->getChildren() as $name => $child) {
            echo '  ', $name, ': ', $child, PHP_EOL;
        }
    } else {
        echo $xml->key(), ' has no children', PHP_EOL;
    }
}

How It Works

  1. The loop visits the two children of library: book and note.
  2. hasChildren() is true for book, because it holds a title element.
  3. For book, getChildren() returns the element so foreach can print its child title.
  4. hasChildren() is false for note, so the else branch runs and getChildren() is not called.

Example 3 — Walk a Whole Tree with Recursion

A small function can use hasChildren() and getChildren() to print every element name, indented by depth.

PHP
<?php
function walk(SimpleXMLElement $node, int $depth = 0): void
{
    for ($node->rewind(); $node->valid(); $node->next()) {
        echo str_repeat('  ', $depth), $node->key(), PHP_EOL;
        if ($node->hasChildren()) {
            walk($node->getChildren(), $depth + 1);
        }
    }
}

$xml = simplexml_load_string(
    '<library>'
    . '<book><title>PHP</title><author>Sam</author></book>'
    . '<book><title>XML</title></book>'
    . '</library>'
);

walk($xml);

How It Works

  1. walk() loops over the children of the element it receives, using the iterator methods.
  2. For each child it prints the element name, indented by two spaces for every level of depth.
  3. When hasChildren() is true, getChildren() gives the element to walk into, and the depth goes up by one.
  4. The first book shows title and author, and the second book shows title, so the output lists the tree in order.

Frequently Asked Questions

It returns the element at the current iterator position so you can loop over its own child elements. It is part of the RecursiveIterator interface.
children() returns the child elements of the element you call it on. getChildren() works from the iterator position and returns the current child. For normal code, children() is easier.
Call it before getChildren(). It tells you whether the current element has child elements, so you only go deeper when there is something to read.
No. You can read nested values directly, such as $xml->book->title, or loop with foreach. getChildren() is mainly useful when you write a recursive function.
The iterator has no position until rewind() runs. After rewind(), check valid() so you know that the iterator points to a real child.

Next: SimpleXMLElement::hasChildren()

SimpleXMLElement::hasChildren() checks whether the element that the iterator currently poi.

SimpleXMLElement::hasChildren() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful