W3docs

getchildren()

SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The SimpleXMLElement::getChildren() function is one of

SimpleXMLElement::children()

SimpleXML is a PHP extension that provides a straightforward API for parsing and manipulating XML documents. Among its methods, SimpleXMLElement::children() lets you retrieve the immediate child elements of a node as SimpleXMLElement objects so you can loop over them. This guide explains what the method returns, how it handles namespaces, and how to use it on real documents.

The older alias getChildren() belongs to the RecursiveIterator interface that SimpleXMLElement implements. In everyday code you call children() directly; this page covers that method.

What SimpleXMLElement::children() returns

children() returns a SimpleXMLElement that you can iterate with foreach to access the direct children of the current node — it does not recurse into grandchildren automatically. It accepts an optional $namespace argument to limit the result to one XML namespace, which is essential when a document mixes vocabularies (for example RSS plus Dublin Core).

Syntax

public function children(?string $namespace = null, bool $isPrefix = false): ?SimpleXMLElement
  • $namespace — a namespace URI (or, when $isPrefix is true, a namespace prefix). When null (the default), children from the current namespace context are returned.
  • $isPrefix — set to true if you pass a prefix (like "bk") instead of the full URI.

Note on default namespaces: When XML uses a default namespace (declared with xmlns="..." and no prefix), you must pass that namespace URI to children() to reach the elements inside it. Calling children(null) on such a node yields nothing.

Basic traversal

The example below loads XML from a string with simplexml_load_string() so it runs without an external file, then walks the tree with nested children() calls. getName() returns each element's tag name:

<?php

$data = <<<XML
<?xml version="1.0"?>
<library>
  <book>
    <title>The PHP Way</title>
    <author>Ada Byte</author>
  </book>
  <book>
    <title>XML in Depth</title>
    <author>Lee Markup</author>
  </book>
</library>
XML;

$xml = simplexml_load_string($data);

foreach ($xml->children() as $book) {
    echo $book->getName() . ":\n";          // "book"
    foreach ($book->children() as $field) {
        echo "  " . $field->getName() . " = " . $field . "\n";
    }
}

Output:

book:
  title = The PHP Way
  author = Ada Byte
book:
  title = XML in Depth
  author = Lee Markup

The outer loop visits each <book>; the inner loop visits its direct children (<title>, <author>). Casting a child to a string (here via concatenation) yields its text content.

Filtering by namespace

When a document declares namespaces, pass the URI to children() to select only the matching elements. getNamespaces(true) returns every namespace in the document keyed by prefix, so you can look the URI up dynamically:

<?php

$data = <<<XML
<?xml version="1.0"?>
<catalog xmlns:bk="http://example.com/books">
  <bk:book>
    <bk:title>Namespaced PHP</bk:title>
  </bk:book>
  <bk:book>
    <bk:title>Beyond SimpleXML</bk:title>
  </bk:book>
</catalog>
XML;

$xml = simplexml_load_string($data);
$ns  = $xml->getNamespaces(true);            // ['bk' => 'http://example.com/books']

foreach ($xml->children($ns['bk']) as $book) {
    echo $book->children($ns['bk'])->title . "\n";
}

Output:

Namespaced PHP
Beyond SimpleXML

Because each <bk:book> and its <bk:title> live in the bk namespace, you must pass that URI at every level to reach them.

Common gotchas

  • It is not recursive. children() only exposes direct children. To walk an entire tree, call it again inside the loop (as shown above) or use an XPath query.
  • Missing namespace filter returns nothing. If a loop is silently empty on namespaced XML, you probably forgot to pass the namespace URI.
  • Text vs. elements. Cast an element to (string) and trim() it when you only want its text content, especially with mixed content nodes.
  • Always validate input. When loading from a file or URL, check for false and enable libxml error handling before trusting the result.

Conclusion

SimpleXMLElement::children() is the standard way to iterate the direct child elements of an XML node in PHP. By combining it with getName() for tag names, namespace URIs for filtering, and string casting for text content, you can navigate nested documents cleanly. For broader context, see the PHP SimpleXML overview and the related attributes() method for reading element attributes.

Practice

Practice
What is the use of the getChildren() function in PHP?
What is the use of the getChildren() function in PHP?
Was this page helpful?